简体   繁体   English

dataframe 根据具有相同索引的值填充空列值(使用 PANDAS)

[英]dataframe fill empty column values based on values with same index (using PANDAS)

I have the following dataframe where the index is the tag# .我有以下dataframe ,其中indextag# I want to fill all of the NaN values in the sound column with the correct values based on matching indexes.我想根据匹配索引用正确的值填充sound列中的所有 NaN 值。

        pet    sound
tag#        
11      cat 
11      cat    meow
11      cat    meow
15      bird   tweet
8       pig    oink
5       dog 
8       pig 
8       pig 
8       pig 
5       dog    woof
5       dog    woof
11      cat    meow

I want the dataframe to look like this我希望 dataframe 看起来像这样

        pet    sound
tag#        
11      cat    meow
11      cat    meow
11      cat    meow
15      bird   tweet
8       pig    oink
5       dog    woof
8       pig    oink 
8       pig    oink 
8       pig    oink 
5       dog    woof
5       dog    woof
11      cat    meow

Try with groupby apply on level 0 and ffill + bfill each group:尝试在级别 0 上使用groupby applyffill + bfill每个组:

df = df.groupby(level=0).apply(lambda g: g.ffill().bfill())
       pet  sound
tag#             
11     cat   meow
11     cat   meow
11     cat   meow
11     cat   meow
15    bird  tweet
8      pig   oink
8      pig   oink
8      pig   oink
8      pig   oink
5      dog   woof
5      dog   woof
5      dog   woof

Complete Working Example:完整的工作示例:

import pandas as pd
from numpy import nan

# Re-create DataFrame
df = pd.DataFrame({
    'tag#': [11, 11, 11, 15, 8, 5, 8, 8, 8, 5, 5, 11],
    'pet': ['cat', 'cat', 'cat', 'bird', 'pig', 'dog', 'pig',
            'pig', 'pig', 'dog', 'dog', 'cat'],
    'sound': [nan, 'meow', 'meow', 'tweet', 'oink', nan, nan,
              nan, nan, 'woof', 'woof', 'meow']
}).set_index('tag#')

df = df.groupby(level=0).apply(lambda g: g.ffill().bfill())
print(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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