fillill和bfill同时在Pandas中使用replace

时间:2017-02-05 13:54:15

标签: python pandas

问题

在Pandas中使用ffill可以同时bfillreplace吗?

请参阅以下示例

l = 12
rng = pd.date_range('1/1/2011', periods=l, freq='8h')
df = pd.DataFrame({ 
    'animals':[0,0,'cat',0,'dog',0,0,0,'mouse',0,'ant',0],
    },index=rng)

df
Out[93]:
                  animals
2011-01-01 00:00:00 0
2011-01-01 08:00:00 0
2011-01-01 16:00:00 cat
2011-01-02 00:00:00 0
2011-01-02 08:00:00 dog
2011-01-02 16:00:00 0
2011-01-03 00:00:00 0
2011-01-03 08:00:00 0
2011-01-03 16:00:00 mouse
2011-01-04 00:00:00 0
2011-01-04 08:00:00 ant
2011-01-04 16:00:00 0

我目前使用replace的两次迭代来执行此操作。

df.animals = df.groupby(df.index.to_datetime().day).transform(lambda x: x.replace(to_replace=0, method='ffill'))
df.animals = df.groupby(df.index.to_datetime().day).transform(lambda x: x.replace(to_replace=0, method='bfill'))

df

                  animals
2011-01-01 00:00:00 cat
2011-01-01 08:00:00 cat
2011-01-01 16:00:00 cat
2011-01-02 00:00:00 dog
2011-01-02 08:00:00 dog
2011-01-02 16:00:00 dog
2011-01-03 00:00:00 mouse
2011-01-03 08:00:00 mouse
2011-01-03 16:00:00 mouse
2011-01-04 00:00:00 ant
2011-01-04 08:00:00 ant
2011-01-04 16:00:00 ant
    ​

它运作正常,但我认为可能有ffillbfill的方法,因此值得检查SO。

1 个答案:

答案 0 :(得分:1)

IIUC你可以这样做:

In [278]: df['animals'] = df['animals'].replace(0, np.nan) \
                                       .groupby(pd.TimeGrouper('D')) \
                                       .bfill().ffill()

In [279]: df
Out[279]:
                    animals
2011-01-01 00:00:00     cat
2011-01-01 08:00:00     cat
2011-01-01 16:00:00     cat
2011-01-02 00:00:00     dog
2011-01-02 08:00:00     dog
2011-01-02 16:00:00     dog
2011-01-03 00:00:00   mouse
2011-01-03 08:00:00   mouse
2011-01-03 16:00:00   mouse
2011-01-04 00:00:00     ant
2011-01-04 08:00:00     ant
2011-01-04 16:00:00     ant