[英]Calculate a new column with Pandas
基於這個問題,我想知道如何使用def()來計算帶有Pandas的新列並使用多個參數(字符串和整數)?
具體例子:
df_joined["IVbest"] = IV(df_joined["Saison"], df_joined["Wald_Typ"], df_joined["NS_Cap"])
“Saison”,“Wald_Typ”是字符串“NS_Cap”是整數
現在我想通過這個定義運行所有這些值並再次返回一個x值:
def IV(saison, wald, ns):
if saison == "Sommer":
if wald == "Laubwald":
x = ns * 0.1
elif wald == "Nadelwald":
x = ns * 0.2
elif wald == "Mischwald":
x = ns * 0.3
elif saison == "Winter":
if wald == "Laubwald":
x = ns * 0.01
elif wald == "Nadelwald":
x = ns * 0.02
elif wald == "Mischwald":
x = ns * 0.03
return x
我怎樣才能做到最好?
我嘗過類似的東西
df_joined["IVbest"] = IV(df_joined["Saison", "Wald_Typ", "NS_Cap"])
要么
df_joined["IVbest"] = df_joined["Saison", "Wald_Typ", "NS_Cap"].apply(IV)
但沒有任何作用:(
我認為在這種情況下最好使用6個掩碼並使用它們來執行這些行的計算:
sommer_laub = (df_joined['Saison'] == 'Sommer') & (df_joined['Wald_Typ'] == 'Laubwald')
sommer_nadel = (df_joined['Saison'] == 'Sommer') & (df_joined['Wald_Typ'] == 'Nadelwald')
sommer_misch = (df_joined['Saison'] == 'Sommer') & (df_joined['Wald_Typ'] == 'Mischwald')
winter_laub = (df_joined['Saison'] == 'Winter') & (df_joined['Wald_Typ'] == 'Laubwald')
winter_nadel = (df_joined['Saison'] == 'Winter') & (df_joined['Wald_Typ'] == 'Nadelwald')
winter_misch = (df_joined['Saison'] == 'Winter') & (df_joined['Wald_Typ'] == 'Mischwald')
df.loc[sommer_laub, 'IVbest'] = df.loc[sommer_laub,'NS_Cap'] * 0.1
df.loc[sommer_nadel, 'IVbest'] = df.loc[sommer_nadel,'NS_Cap'] * 0.2
df.loc[sommer_misch, 'IVbest'] = df.loc[sommer_misch,'NS_Cap'] * 0.3
df.loc[winter_laub, 'IVbest'] = df.loc[winter_laub,'NS_Cap'] * 0.01
df.loc[winter_nadel, 'IVbest'] = df.loc[winter_nadel,'NS_Cap'] * 0.02
df.loc[winter_misch, 'IVbest'] = df.loc[winter_misch,'NS_Cap'] * 0.03
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.