用Pandas计算新列 [英] Calculate a new column with Pandas

查看:208
本文介绍了用Pandas计算新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个问题,我想知道如何使用def()计算新栏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)?

具体示例:

df_joined["IVbest"] = IV(df_joined["Saison"], df_joined["Wald_Typ"], df_joined["NS_Cap"])

Saison,Wald_Typ是字符串NS_Cap是一个整数

"Saison", "Wald_Typ" are strings "NS_Cap" is an integer

所有这些值通过这个定义并返回一个x值:

Now I want to run all those values through this definition and return me again an x-value:

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个掩码,并使用这些行在这些行上执行计算:

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

这篇关于用Pandas计算新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆