【发布时间】:2021-11-12 16:06:21
【问题描述】:
我有以下数据框有一个棘手的问题:
df = pd.DataFrame({'weight': [[200, 190, 188, 180, 170],
[181, 175, 172, 165, 150]],
'days_since_gym': [[0, 87, 174, 205, 279],
[43, 171, 241, 273, 300]]})
print(df)
weight days_since_gym
0 [200, 190, 188, 180, 170] [0, 91, 174, 205, 279]
1 [181, 175, 172, 165, 150] [93, 171, 241, 273, 300]
我必须根据以下条件制作 4 列(0-90 天、91-180 天、181-270 天、271-360 天):
1) If there are multiple weights in a specific time duration, get the maximum weight in that time duration column.
2) If no weight is present in that time duration, the value for that duration would be 0.
期望的输出:
weight days_since_gym 0-90 91-180 181-270 271-360
0 [200, 190, 188, 180, 170] [0, 87, 174, 205, 279] 200 188 180 170
1 [181, 175, 172, 165, 150] [93, 171, 241, 273, 300] 0 181 172 165
最聪明的做法是什么?任何建议,将不胜感激。谢谢!
【问题讨论】:
-
days_since_gym的条目在您的第一个代码块和您的最后一个代码块 jsyk 中不同
标签: python pandas dataframe timedelta