熊猫根据另一列将nan替换为第一个非nan值

时间:2020-01-30 07:21:27

标签: python pandas dataframe for-loop

我具有以下形式的数据框:(除了这些列以外,还有很多列-为简洁起见,将其删除)

import pandas as pd

headers = ['A','B','C']
data = [['p1','','v1'],
        ['p2','','ba'],
        ['p3',9,'fg'],
        ['p1',1,'fg'],
        ['p2',45,'af'],
        ['p3',1,'fg'],
        ['p1',1,'hf']
        ]

df = pd.DataFrame(data,columns=headers)

    A   B   C
0  p1      v1
1  p2      ba
2  p3   9  fg
3  p1   1  fg
4  p2  45  af
5  p3   1  fg
6  p1   1  hf

B列中有重复项,因此 latest 值应为非NA(但可能不是)

我想用最新的非NA值替换col B值。像这样:

unique_people = df['A'].unique()
for person in unique_people:
    sub_df = df[df['A'] == person]
    val = sub_df['B'].tail(1).values
    df['A'][df['A'] == person] = val  # this also doesnt work because its not inplace

我确定有更好的方法可以做到,但是我不确定如何做。谁能指出更好的方法?

谢谢!

1 个答案:

答案 0 :(得分:1)

首先将空字符串替换为缺失值,然后将GroupBy.transformGroupBy.last一起用于每个组的最后一个非缺失值:

headers = ['A','B','C']
data = [['p1','','v1'],
        ['p2','','ba'],
        ['p3',9,'fg'],
        ['p1',1,'fg'],
        ['p2',45,'af'],
        ['p3',1,'fg'],
        ['p1','','hf']
        ]

df = pd.DataFrame(data,columns=headers)

df['B'] = df['B'].replace('', np.nan)

df['B'] = df.groupby('A')['B'].transform('last')
print (df)
    A     B   C
0  p1   1.0  v1
1  p2  45.0  ba
2  p3   1.0  fg
3  p1   1.0  fg
4  p2  45.0  af
5  p3   1.0  fg
6  p1   1.0  hf