简体   繁体   English

熊猫中的多索引fillna

[英]Multi-Indexed fillna in Pandas

I have a multi-indexed dataframe and I'm looking to backfill missing values within a group. 我有一个多索引的数据框,我想回填一组中的缺失值。 The dataframe I have currently looks like this: 我目前拥有的数据框如下所示:

df = pd.DataFrame({
                'group': ['group_a'] * 7 + ['group_b'] * 3 + ['group_c'] * 2,
                'Date': ["2013-06-11",
                        "2013-07-02",
                        "2013-07-09",
                        "2013-07-30",
                        "2013-08-06",
                        "2013-09-03",
                        "2013-10-01",
                        "2013-07-09",
                        "2013-08-06",
                        "2013-09-03",
                        "2013-07-09",
                        "2013-09-03"],
                 'Value': [np.nan, np.nan, np.nan,  9,  4, 40, 18, np.nan, np.nan, 5, np.nan, 2]})

df.Date = df['Date'].apply(lambda x: pd.to_datetime(x).date())
df = df.set_index(['group', 'Date'])

I'm trying to get a dataframe that backfills the missing values within the group. 我正在尝试获取一个数据框,以回填该组中缺少的值。 Like this: 像这样:

Group   Date        Value
group_a 2013-06-11      9
        2013-07-02      9
        2013-07-09      9
        2013-07-30      9
        2013-08-06      4
        2013-09-03     40
        2013-10-01     18
group_b 2013-07-09      5
        2013-08-06      5
        2013-09-03      5
group_c 2013-07-09      2
        2013-09-03      2

I tried using pd.fillna('Value', inplace=True) , but I get a warning on setting a value on copy, which I've since figured out is related to the presence of the multi-index. 我尝试使用pd.fillna('Value', inplace=True) ,但在复制时设置了一个警告,此后我就发现与多索引的存在有关。 Is there a way to make fillna work for multi-indexed rows? 有没有一种方法可以使fillna适用于多索引行? Also, ideally I'd be able to apply the fillna to only one column and not the entire dataframe. 另外,理想情况下,我将能够将fillna仅应用于一列,而不应用于整个数据框。

Any insight on this would be great. 任何对此的见识都将是巨大的。

Use groupby(level=0) then bfill and update : 使用groupby(level=0)然后bfillupdate

df.update(df.groupby(level=0).bfill())
df

Note: update changes df inplace. 注意:原位update df

在此处输入图片说明

Other alternatives 其他选择

df = df.groupby(level='group').bfill()

df = df.unstack(0).bfill().stack().swaplevel(0, 1).reindex_like(df)

Column specific 列特定

df.Value = df.groupby(level=0).Value.bfill()

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

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