![](/img/trans.png)
[英]performing multiple string operations on a pandas Series/DataFrame
[英]Performing mathematical operations on a pandas dataframe
该列看起来像
Mod_month Mod_year Reg_Year Reg_Month
10 2016 2016 10
1 2018 2016 12
2 2017 2017 2
我想对数据框的列执行一些数学运算以计算日期之间的差异。
我试过使用:
df['difference']=df[df['mod_month']-df['last_month']+df['mod_month']*12-df['last_year']]
返回错误:
KeyError: '[-1896 -2015 -1993 ... -1955 -1877 -1981] not in index'
我认为这是由于null值造成的,我也尝试使用了coerce = 'True'
,它返回了invalid syntax
。
我看过其他帖子,但没有一个出现我所犯的错误,因此,我们将不胜感激。
我认为需要删除df[]
,因为它是boolean indexing
或按subset
选择的语法:
df['difference'] = df['mod_month'] - df['last_month'] + df['mod_month'] * 12 - df['last_year']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.