如何在数据框中添加新的计算列? [英] How can I add a new computed column in a dataframe?

查看:253
本文介绍了如何在数据框中添加新的计算列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据我拥有的数据来计算一个人的年龄:

I'm trying to compute the age of a person from the data that I have:

Data columns in 'Person' Dataframe:
TodaysDate   non-null datetime64[ns]
YOB          non-null float64

所以我想在该数据框中创建一个称为年龄"的新列,到目前为止,我有以下代码:

So I want to make a new column inside that dataframe called 'Age' and so far I have the following code:

Person['Age'] = map(sum, (Person.ix[0,'TodaysDate']).year, -(Person['YOB']))

TypeError: 'int' object is not iterable

我也尝试过:

Person['Age'] = map((Person.ix[0,'TodaysDate']).year - Person['YOB'])

TypeError: map() must have at least two arguments.

我尝试了在其他问题上发布的几种不同方法,但似乎没有一种有效.这似乎很简单...但是无法正常工作.

I've tried a few different methods that were posted on other questions but none seem to work. This seems very simple to do...but can't get it to work.

有什么想法可以使用map函数从float列YOB中减去datetime列TodaysDate并将值放入Age列中?我想对数据框中的每一行执行此操作.

Any ideas how I can use the map function to subtract the datetime column TodaysDate from the float column YOB to and put the value into Age column? I'd like to do this for every row in the dataframe.

谢谢!

推荐答案

数据:

In [5]: df
Out[5]:
    YOB
0  1955
1  1965
2  1975
3  1985

您不需要额外的列TodaysDate-您可以动态获取它:

you don't need an extra column TodaysDate - you can get it dynamically:

In [6]: df['Age'] = pd.datetime.now().year - df.YOB

In [7]: df
Out[7]:
    YOB  Age
0  1955   62
1  1965   52
2  1975   42
3  1985   32

或者,您可以使用 DataFrame.eval() 方法:

Alternatively you can use DataFrame.eval() method:

In [16]: df
Out[16]:
    YOB
0  1955
1  1965
2  1975
3  1985

In [17]: df.eval("Age = @pd.datetime.now().year - YOB", inplace=True)

In [18]: df
Out[18]:
    YOB  Age
0  1955   62
1  1965   52
2  1975   42
3  1985   32

这篇关于如何在数据框中添加新的计算列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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