[英]Pandas: Create New Column Based on Conditions of Multiple Columns
I have the following dataset:我有以下数据集:
ID AAA BBB CCC DDD
1234 {'2015-01-01': 1} {'2016-01-01': 1, {'2015-01-02': 1} {'2016-01-02': 1}
'2016-02-15': 2}
1235 {'2017-11-05': 1, {'2018-01-05': 1} NaN {'2017-01-06': 1}
'2018-06-05': 1}
In the cell, 'key' is the date when someone is hospitalized and 'value' is the number of days.在单元格中,“key”是某人住院的日期,“value”是天数。
I need to create a new column for hospitalization ('Yes' or 'No').我需要为住院创建一个新列(“是”或“否”)。
The condition to be 'yes': “是”的条件:
For example, if [AAA or BBB] has a date of January 01, 2020. For 'yes', the date in [CCC or DDD] should be January 02, 2020.例如,如果 [AAA 或 BBB] 的日期为 2020 年 1 月 1 日。如果选择“是”,则 [CCC 或 DDD] 中的日期应为 2020 年 1 月 2 日。
Desired output:所需的 output:
ID AAA BBB CCC DDD Hospitalized
1234 {'2015-01-01': 1} {'2016-01-01': 1, {'2015-01-02': 1} {'2016-01-02': 1} Yes
'2016-02-15': 2}
1235 {'2017-11-05': 1, {'2018-01-05': 1} NaN NaN No
'2018-06-05': 1}
1236 {'2017-11-05': 1, {'2018-01-05': 1} NaN {'2018-01-06': 1} Yes
'2018-06-05': 1}
I have tried the following code, but this captures if the dates are present but doesn't capture the timestamp.我尝试了以下代码,但这会捕获日期是否存在但不捕获时间戳。
df['hospitalized'] = (df
.apply(lambda r: 'yes' if (1 if pd.notna(r.loc[['AAA', 'BBB']]).any() else 0) +
(1 if pd.notna(r.loc[['CCC', 'DDD']]).any() else 0) > 1
else 'no', axis=1))
Any suggestions would be appreciated.任何建议,将不胜感激。 Thanks!
谢谢!
df:东风:
df = pd.DataFrame([[1234, {'2015-01-01': 1}, {'2016-01-01': 1, '2016-02-15': 2}, {'2015-01-02': 1}, {'2016-01-02': 1}], [1235, {'2017-11-05': 1,'2018-06-05': 1}, {'2018-01-05': 1}, np.nan, np.nan]], columns= ['ID', 'AAA', 'BBB', 'CCC', 'DDD'])
Try:尝试:
import itertools
from dateutil import parser
import datetime
def func(x):
A_B_dates = list(map(parser.parse,list(itertools.chain(*[x['AAA'].keys()] + [x['BBB'].keys()]))))
C_D_dates = list(map(parser.parse,list(itertools.chain(*[x['CCC'].keys()] + [x['DDD'].keys()]))))
for date1 in A_B_dates:
if date1+datetime.timedelta(days=1) in C_D_dates:
return 'yes'
return 'no'
df = df.where(df.notna(), lambda x: [{}])
df['Hospitalised'] = df.apply(func, axis=1)
df:东风:
ID AAA BBB CCC DDD Hospitalised
0 1234 {'2015-01-01': 1} {'2016-01-01': 1, '2016-02-15': 2} {'2015-01-02': 1} {'2016-01-02': 1} yes
1 1235 {'2017-11-05': 1, '2018-06-05': 1} {'2018-01-05': 1} {} {'2017-01-06': 1} no
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.