[英]How to populate a column based another column in python?
I have a df that look like this:我有一个看起来像这样的 df:
ID Test Done Test Action Test Date
1234 Happy Test Decline 2021-11-30
1234 None Decline None
1235 Sad Test Decline 2022-03-24
1235 None Decline 2022-03-04
1235 None Decline 2022-03-04
1236 Lonely Test Decline 2022-05-06
1236 Lonely Test Decline 2022-05-06
1236 Lonely Test Decline 2022-05-06
I am trying to populate all the None or empty fields in Test Done that are associated with an ID number.我正在尝试填充 Test Done 中与 ID 号关联的所有 None 或空字段。 So I want my df to look like....所以我希望我的 df 看起来像......
ID Test Done Test Action Test Date
1234 Happy Test Decline 2021-11-30
1234 Happy Test Decline None
1235 Sad Test Decline 2022-03-24
1235 Sad Test Decline 2022-03-04
1235 Sad Test Decline 2022-03-04
1236 Lonely Test Decline 2022-05-06
1236 Lonely Test Decline 2022-05-06
1236 Lonely Test Decline 2022-05-06
I am not sure how to go about this.我不确定如何与 go 联系。 From what I searched on the web, I did not find anything that related to this specific question I had or find any functions that could answer my question.从我在 web 上搜索的内容中,我没有找到与我遇到的这个特定问题相关的任何内容,也没有找到可以回答我的问题的任何功能。
Edit:编辑:
I want to populate just the None values with the first value that is shown in Test Done.我只想用测试完成中显示的第一个值填充 None 值。 So for instance in the example the first value is Happy Test with ID 1234, I wand the None value to be Happy Test, the same goes for Sad Test with 1235 ID.因此,例如在示例中,第一个值是 ID 为 1234 的 Happy Test,我希望 None 值是 Happy Test,同样适用于 ID 为 1235 的 Sad Test。 If an ID already has a Test Done populated then we can skip that.如果 ID 已经填充了测试完成,那么我们可以跳过它。 Hope this makes sense.希望这是有道理的。
Use a groupby with ffill()
.将 groupby 与ffill()
结合使用。
data = {'id': [1234, 1234, 1235, 1235, 1235, 1236, 1236, 1236],
'test': ['Happy Test',
None,
'Sad Test',
None,
None,
'Lonely Test',
'Lonely Test',
'Lonely Test']
}
df = pd.DataFrame(data)
df['test'] = df.groupby('id')['test'].ffill()
Output: Output:
id test
0 1234 Happy Test
1 1234 Happy Test
2 1235 Sad Test
3 1235 Sad Test
4 1235 Sad Test
5 1236 Lonely Test
6 1236 Lonely Test
7 1236 Lonely Test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.