Skip to content Skip to sidebar Skip to footer

Calculate A New Column With Pandas

Based on this Question, I would like to know how can I use a def() to calculate a new column with Pandas and use more than one arguments (strings and integers)? Concrete example: d

Solution 1:

I think in this case it would be better to use 6 masks and use these to perform the calculations just on those rows:

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

Post a Comment for "Calculate A New Column With Pandas"