[英]Pandas Replace original column null values with df.apply results
I have below dataframe df
, where stamp B
are null sometimes.我有以下 dataframe
df
,其中stamp B
有时是 null 。 Have to fill such null values with date of Stamp A
and respective time from the Time
column必须使用
Stamp A
的日期和Time
列中的相应时间填充此类 null 值
stamp A stamp B Time
0 2012-10-08 18:15:05 2012-10-08 18:15:05 19:00:01
1 2012-10-09 12:15:05 NaT 18:45:09
2 2012-10-11 18:13:00 NaT 12:20:20
3 2012-10-11 08:15:15 2012-10-11 18:15:05 22:10:05
4 2012-10-12 18:15:20 2012-10-12 17:10:20 19:34:12
Here is my solution -这是我的解决方案 -
>>>from datetime import dateime as dtm
>>>result = df[df['stamp B'].isnull()].apply(lambda x: dtm.combine(x['stamp A'].date(), dtm.strptime(x["Time"], "%H:%M:%S").time()), axis=1)
It returns result
as below:它返回
result
如下:
1 2012-10-09 18:45:09
2 2012-10-11 12:20:20
dtype: datetime64[ns]
But not sure, how to replace this result
with NaT
values in the original dataframe df['stamp B']
但不确定,如何用原始 dataframe
df['stamp B']
中的NaT
值替换此result
I would extract the date from stamp A
, add the Time
, then do a fillna
on stamp B
:我会从
stamp A
中提取日期,添加Time
,然后在stamp B
上做一个fillna
:
s = df['stamp A'].dt.normalized() + pd.to_timedelta(df['Time'])
df['stamp B'] = df['stamp B'].fillna(s)
Use Series.dt.floor
for remove times and add timedeltas by to_timedelta
and then replace missing values by Series.combine_first
:使用
Series.dt.floor
删除时间并通过to_timedelta
添加 timedeltas,然后通过Series.combine_first
替换缺失值:
dates = df['stamp A'].dt.floor('d').add(pd.to_timedelta(df['Time']))
df['stamp B'] = df['stamp B'].combine_first(dates)
print (df)
stamp A stamp B Time
0 2012-10-08 18:15:05 2012-10-08 18:15:05 19:00:01
1 2012-10-09 12:15:05 2012-10-09 18:45:09 18:45:09
2 2012-10-11 18:13:00 2012-10-11 12:20:20 12:20:20
3 2012-10-11 08:15:15 2012-10-11 18:15:05 22:10:05
4 2012-10-12 18:15:20 2012-10-12 17:10:20 19:34:12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.