熊猫DataFrame groupby fillna inplace

时间:2019-07-11 15:55:34

标签: python pandas dataframe pandas-groupby missing-data

请让我知道如何在Pandas DataFrame groupby中适当地填写缺失值吗?不知何故,当我不适当地填充它们时,它可以工作,但是给出inplace=True并不会更改输入DataFrame。下面是一个示例代码:

import pandas as pd
t = pd.DataFrame(data={'i0':[0,0,0,0,1,1,1,1,2,2,2,2],
                       'i1':[0,1,2,3,0,1,2,3,0,1,2,3],
                       'i2':['A']*12,
                       'x':[10.,20.,30.,np.nan,np.nan,21.,
                            np.nan,41.,np.nan,np.nan,32.,42.]})
t.set_index(['i0','i1','i2'], inplace=True)
t.sort_index(inplace=True)

# works
t = t.groupby(level='i1').fillna(method='ffill')

# does not work
t.groupby(level='i1').fillna(method='ffill', inplace=True)

谢谢您的帮助!

0 个答案:

没有答案