简体   繁体   English

根据另一列的“组”值填充 dataframe 的列

[英]Filling column of dataframe based on 'groups' of values of another column

I am trying to fill values of a column based on the value of another column.我正在尝试根据另一列的值填充列的值。 Suppose I have the following dataframe:假设我有以下 dataframe:

import pandas as pd
data = {'A': [4, 4, 5, 6],
        'B': ['a', np.nan, np.nan, 'd']}
df = pd.DataFrame(data)

And I would like to fill column B but only if the value of column A equals 4. Hence, all rows that have the same value as another in column A should have the same value in column B (by filling this).而且我想填充 B 列,但前提是 A 列的值等于 4。因此,与 A 列中的另一个具有相同值的所有行在 B 列中应该具有相同的值(通过填充这个)。

Thus, the desired output should be:因此,所需的 output 应该是:

data = {'A': [4, 4, 5, 6],
        'B': ['a', a, np.nan, 'd']}
df = pd.DataFrame(data)

I am aware of the fillna method, but this gives the wrong output as the third row also gets the value 'A' assigned:我知道 fillna 方法,但这给出了错误的 output 因为第三行也分配了值“A”:

df['B'] = fillna(method="ffill", inplace=True)
data = {'A': [4, 4, 5, 6],
        'B': ['a', 'a', 'a', 'd']}
df = pd.DataFrame(data)

How can I get the desired output?如何获得所需的 output?

Try this:尝试这个:

df['B'] = df.groupby('A')['B'].ffill()

Output: Output:

>>> df
   A    B
0  4    a
1  4    a
2  5  NaN
3  6    d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM