我有如下的DataFrame:
rng = pd.date_range('2020-12-01', periods=5, freq='D')
df = pd.DataFrame({"ID" : [1, 2, 2, 1, 3],
"status" : ["acc", "rem", "rem", "acc", "other"], "date" : rng})我需要用列创建DataFrame:
结果如下:

发布于 2020-12-28 13:12:44
如下所示:
In [2608]: t = pd.to_datetime('today').normalize()
In [2615]: In [2627]: x = abs(df.groupby(['ID', 'status'])['date'].max() - t).dt.days.reset_index()
In [2619]: y = x.pivot('ID', 'status', 'date')
In [2620]: y
Out[2620]:
status acc other rem
ID
1 24.0 NaN NaN
2 NaN NaN 25.0
3 NaN 23.0 NaN备注:您可以将acc、rem重命名为New1和New2。为了得到更多的理解,我一直保留着它。
发布于 2020-12-28 13:26:37
代码:
df=df.groupby(['status'])['date'].agg('last').reset_index()
df['diff']=abs(pd.to_datetime('today').day-df['date'].dt.day)
df_final=df.pivot(columns='status',values='diff')产出:
df_final
Out[104]:
status acc other rem
0 24.0 NaN NaN
1 NaN 23.0 NaN
2 NaN NaN 25.0https://stackoverflow.com/questions/65477883
复制相似问题