我有以下功能:
def create_col4(df):
df['col4'] = df['col1'] df['col2']
如果我在我的 jupyter 筆記本中應用此功能,如
create_col4(df_test)
,df_test不斷地被 修正col4。
但是,如果我有以下代碼應用 numpy 函式:
import numpy as np
def create_col4(df):
df['col4'] = np.where(df[col1] == 1, True, False)
,
create_col4(df_test)
既不持久追加也不拋出錯誤df_test。col4
為什么是這樣?
如果原因在個別代碼中,則完整的用例代碼:
在職的:
def create_leg(df):
df['leg'] = df["dep_ap_sched"] "-" df["arr_ap_sched"]
直接在 jupyter notebook 中作業時也可以:
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
)
不作業:
create_rotmismatch(some_df)
在哪里
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
)
uj5u.com熱心網友回復:
import numpy as np
def create_col4(df_test):
df['col4'] = np.where(df[col1] == 1, True, False)
沒有進一步檢查,我首先看到的是這個。要么df_test,要么,df但你在這里混合名稱。
將其更改為:
import numpy as np
def create_col4(df):
df['col4'] = np.where(df[col1] == 1, True, False)
關于您的其他問題,請嘗試在函式結束時回傳 df 。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471433.html
下一篇:分配給雙索引numpy陣列
