假设我有以下DataFrame:

df = pd.DataFrame({'player': ['LBJ', 'LBJ', 'LBJ', 'Kyrie', 'Kyrie', 'LBJ', 'LBJ'],
                   'points': [25, 32, 26, 21, 29, 21, 35]})

如何执行ffill相反的操作,以便获得以下DataFrame:
df = pd.DataFrame({'player': ['LBJ', np.nan, np.nan, 'Kyrie', np.nan, 'LBJ', np.nan],
                   'points': [25, 32, 26, 21, 29, 21, 35]})

也就是说,我想用NaN直接填充重复的值。

到目前为止,这是我想要的,但是我希望有一个内置的pandas方法或更好的方法:
for i, (index, row) in enumerate(df.iterrows()):
    if i == 0:
        continue
    go_back = 1
    while True:
        past_player = df.ix[i-go_back, 'player']
        if pd.isnull(past_player):
            go_back += 1
            continue
        if row['player'] == past_player:
            df.set_value(index, 'player', value=np.nan)
        break

最佳答案

ffinv = lambda s: s.mask(s == s.shift())
df.assign(player=ffinv(df.player))

  player  points
0    LBJ      25
1    NaN      32
2    NaN      26
3  Kyrie      21
4    NaN      29
5    LBJ      21
6    NaN      35

关于python - 执行与 Pandas 填充相反的操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46479437/

10-13 00:03