【发布时间】:2020-09-09 14:27:07
【问题描述】:
使用带有 python 的 pandas 将列值与相邻列值映射 有一个带有值的df
name exm_date att_date
tom 2019-03-05 11:48:03.166 2020-03-05 11:48:03.166
mark 2018-03-05 11:48:03.166 2020-03-05 11:48:03.166
matt 2020-08-05 11:48:03.166 NAT
rob 2020-06-05 11:48:03.166 NAT
chuck 2020-02-05 11:48:03.166 NAT
tom 2020-03-05 11:48:03.166 NAT
matt 2020-02-05 11:48:03.166 2020-03-05 11:48:03.166
chuck 2020-06-05 11:48:03.166 2020-03-05 11:48:03.166
对于 att_date 中具有 NAT 的值,应从 exm_date 中选择日期 预期输出:
name exm_date att_date
tom 2019-03-05 11:48:03.166 2020-03-05 11:48:03.166
mark 2018-03-05 11:48:03.166 2020-03-05 11:48:03.166
matt 2020-08-05 11:48:03.166 2020-08-05 11:48:03.166
rob 2020-06-05 11:48:03.166 2020-06-05 11:48:03.166
chuck 2020-02-05 11:48:03.166 2020-02-05 11:48:03.166
tom 2020-03-05 11:48:03.166 2020-03-05 11:48:03.166
matt 2020-02-05 11:48:03.166 2020-03-05 11:48:03.166
chuck 2020-06-05 11:48:03.166 2020-03-05 11:48:03.166
【问题讨论】:
-
如果 NAT 不是文本,并且实际上为空,
df.ffill(axis=1)将在同一行使用 exm_date 值填充 att_date。
标签: python python-3.x pandas dataframe pandas-groupby