【问题标题】:How to calculate age in python from a file? [duplicate]如何从文件中计算python中的年龄? [复制]
【发布时间】:2019-12-14 14:22:03
【问题描述】:

这是我的文件的样子

姓名性别出生死亡
约翰男 1940 1990
史蒂夫男 1953 1985
约翰男 1933 年 1965 年
莎拉女 1917 1955

我已经尝试了这些方法并遇到了一些错误。

file = pd.read_csv('file')

df.insert(6, "Age", file['Death - Birth'])
age = pd.Series([])

for i in range(len(df)):
age[i] = df['Death Year - Birth Year']

我想添加一个年龄列,该列将根据出生和死亡年份计算年龄。

【问题讨论】:

标签: python python-3.x pandas


【解决方案1】:

以下应该是一个简单的修复:

df = pd.read_csv('file.csv')

df['age'] = df['death'] - df['birth']

【讨论】:

    猜你喜欢
    • 2015-01-02
    • 2019-04-04
    • 1970-01-01
    • 2017-08-08
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多