更新时间:2023-11-18 22:12:28
数据:
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