I have the following function:
def create_col4(df):
df['col4'] = df['col1'] df['col2']
If I apply this function within my jupyter notebook as in
create_col4(df_test)
, df_test
is persistently amended by col4
.
However, if I have the following code where I apply a numpy function:
import numpy as np
def create_col4(df):
df['col4'] = np.where(df[col1] == 1, True, False)
,
create_col4(df_test)
does neither persistently append df_test
by col4
nor does it throw an error.
Why is this?
The full use case code in case the reason is in the individual code:
working:
def create_leg(df):
df['leg'] = df["dep_ap_sched"] "-" df["arr_ap_sched"]
also working when doing in the jupyter notebook directly:
df['rot_mismatch'] = np.where(
df['ac_registration_x'].shift(-1).eq(df['ac_registration_x']) == True,
~df['dep_ap_sched'].shift(-1).eq(df['arr_ap_sched']),
False
)
not working:
create_rotmismatch(some_df)
where
def create_rotmismatch(df):
df['rot_mismatch'] = np.where(
df['ac_registration_x'].shift(-1).eq(df['ac_registration_x']) == True,
~df['dep_ap_sched'].shift(-1).eq(df['arr_ap_sched']),
False
)
CodePudding user response:
import numpy as np
def create_col4(df_test):
df['col4'] = np.where(df[col1] == 1, True, False)
Without inspecting further what I first saw was this.
Either df_test
or df
but you mix names here.
Change it to:
import numpy as np
def create_col4(df):
df['col4'] = np.where(df[col1] == 1, True, False)
About your other concerns, try to return the df at the end of your function.
def create_rotmismatch(df):
df['rot_mismatch'] = np.where(
df['ac_registration_x'].shift(-1).eq(df['ac_registration_x']) == True,
~df['dep_ap_sched'].shift(-1).eq(df['arr_ap_sched']),
False
)
return df
df = create_rotmismatch(df)