简体   繁体   中英

fillna() for Multi-Index Pandas DataFrame

I have a multi-index Pandas dataframe and I want to use ffill() to fill any NaNs in certain columns. Following code shows the structure of the sample dataframe, and the result of ffill() in the next snapshot.

room = ['A', 'B']
val = range(3)
df = pd.DataFrame(columns=pd.MultiIndex.from_product([room, val]),data=np.random.randn(3,6))
df.loc[1,('B',0)]=np.nan
# print(df.loc[1,('B',0)])
display(df)
df = df.ffill(axis=1)
display(df)

在此输入图像描述

What I was hoping to get is that the NaN at [1,('B',0)] is replaced with -0.392674 and not with -1.349675. Generally, I want to be able to ffill() from the corresponding columns from level 1 ([0,1,2]).

How do I achieve this?

I think you are looking for groupby fillna

df=df.groupby(level=1,axis=1).fillna(method='ffill')
df
Out[496]: 
          A                             B                    
          0         1         2         0         1         2
0 -0.177358 -1.531091 -0.945004  1.665143  0.602459 -0.008192
1 -0.006995  0.472267 -0.859471 -0.006995 -0.601538 -0.410391
2  0.101494  1.031941  0.499288  0.804391 -0.224750 -0.778403

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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