[英]Applying age function to data-frame column
I have a dataframe df
with a column date of birth
. 我有一个
date of birth
df
的数据框df
。 The head looks like: 头看起来像:
Date of birth
0 1957-04-30
1 1966-11-10
2 1966-11-10
3 NOT KNOWN
4 1958-10-28
5 1958-06-04
I also have a variable called referencePeriodEndDate
which is a date in a yyyy-mm-dd
format and for example looks like 2017-03-31
我还有一个名为
referencePeriodEndDate
的变量,它是yyyy-mm-dd
格式的日期,例如,看起来像2017-03-31
I am trying to create a new column called Age
which is the age of from the Date of birth
upto the referencePeriodEndDate
我正在尝试创建一个名为
Age
的新列,该列是从Date of birth
到referencePeriodEndDate
的年龄
so the function to apply to each row would looks like: 因此,应用于每行的函数应如下所示:
(`referencePeriodEndDate` - df["Date of birth"]) / 365.25
There is the potential for rows in the Date of birth
column to be empty ( null
) or have the entry 'NOT KNOWN'
so I need to return the value 'NOT KNOWN'
to the effected column
in the new Age column. 没有为行中潜在的
Date of birth
栏是空的( null
)或具有进入'NOT KNOWN'
,所以我需要返回的值'NOT KNOWN'
的影响column
在新时代的列。
I have come up with the following but it refuses to work (or return an error) 我想出了以下内容,但它拒绝工作(或返回错误)
df["Age"].apply(lambda row: TimeCalc(df,referencePeriodEndDate) if row.notnull() else "NOT KNOWN")
def TimeCalc(rawDatabase,referencePeriodEndDate):
Age = ((referencePeriodEndDate - rawDatabase["Date of birth"]) / 365.25)
return Age
The desired output would look like: 所需的输出如下所示:
Date of birth Age
30/04/1957 59.91786448
10/11/1966 50.38740589
10/11/1966 50.38740589
NOT KNOWN NOT KNOWN
28/10/1958 58.42299795
04/06/1958 58.82272416
You can using to_datetime
您可以使用
to_datetime
df['Dateofbirth']=pd.to_datetime(df['Dateofbirth'],errors='coerce')
df['Age']=(pd.to_datetime('2017-03-31')-df['Dateofbirth']).dt.days/365.25
df.fillna('unknow')
Out[370]:
Dateofbirth Age
0 1957-04-30 00:00:00 59.9179
1 1966-11-10 00:00:00 50.3874
2 1966-11-10 00:00:00 50.3874
3 unknow unknow
4 1958-10-28 00:00:00 58.423
5 1958-06-04 00:00:00 58.8227
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.