【发布时间】: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']
我想添加一个年龄列,该列将根据出生和死亡年份计算年龄。
【问题讨论】:
-
您遇到了什么错误?请包含所有相关代码和数据,以及对调试工作的说明。请参阅:minimal reproducible example、meta.stackoverflow.com/q/261592/11301900、ericlippert.com/2014/03/05/how-to-debug-small-programs。
-
我建议您阅读 Pandas 文档,您的代码是单一的,并且由于对该库的一些了解,可以大大简化。
标签: python python-3.x pandas