【发布时间】:2020-07-20 14:35:05
【问题描述】:
类似于this question,我有一个功能“preWeight”,它对每个 MotherID 都有多个观察值,我想将它转换为数据帧到一个新的数据帧,其中
- 如果 preWeight>=4000 对于特定的 MotherID,无论剩余的观察结果如何,我都会将 preWeight 的值分配为“是”
- 否则,如果特定 MotherID 的 preWeight
所以我想转换这个数据框:
ChildID MotherID preWeight
0 20 455 3500
1 20 455 4040
2 13 102 2500
3 13 102 NaN
4 702 946 5000
5 82 571 2000
6 82 571 3500
7 82 571 3800
进入这个:
ChildID MotherID preWeight
0 20 455 Yes
1 13 102 No
2 702 946 Yes
3 82 571 No
我试过这个:
df.groupby('MotherID')['preWeight'].apply(
lambda x: 'Yes' if x>4000 in x.values else 'No').reset_index()
我收到以下错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
提前致谢。
【问题讨论】:
-
对于相同的
ChildID和MotherID,如果preWeight一次低于4000 一次高于4000,preWeight应该有什么值?