简体   繁体   English

如何根据 python 中的另一列填充一列?

[英]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.

相关问题 如何根据另一列中的数据填充 dataframe 中的列并在 python 中的另一列上进行条件/切换 - How to populate a column in a dataframe based on data in another column and condition /switch on another column in python 根据另一个 dataframe 中列的 if 语句填充 dataframe 中的列 - Python - Populate a column in a dataframe based on if statement for column in another dataframe - Python 如何根据另一列中满足的条件填充 dataframe 列 - How to populate a dataframe column based on condition met in another column 根据另一列中的条件填充新列 - Populate new column based on conditions in another column 根据PySpark中的另一列填充不同的列 - Populate distinct of column based on another column in PySpark 如何根据 Python 中的另一个数据框列填充列? - How to populate a column base on another dataframe column in Python? 根据 Python 中的条件在另一行和另一列中填充新列,添加日期 - Populate new column with added day to date in another row and and another column based on condition in Python 如何根据另一列中的值从另一列中填充一列中的值? - How to populate value in one column from another column based on vale in other column? Pandas - 根据另一个填充一个数据框列 - Pandas - populate one dataframe column based on another 如何根据 python 中多列的值填充一列? - How to populate a column based on values from multiple columns in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM