问题描述
我有一个多索引数据框,我正在寻找回填组中缺失值的方法.我目前拥有的数据框如下所示:
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
我尝试使用pd.fillna('Value', inplace=True)
,但是收到关于在副本上设置值的警告,此后我就发现与多索引的存在有关.有没有一种方法可以使fillna适用于多索引行?另外,理想情况下,我只能将fillna应用于一列,而不是整个数据框.
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. Is there a way to make fillna work for multi-indexed rows? Also, ideally I'd be able to apply the fillna to only one column and not the entire dataframe.
任何对此的见识都会很棒.
Any insight on this would be great.
推荐答案
使用groupby(level=0)
然后使用bfill
和update
:
df.update(df.groupby(level=0).bfill())
df
注意:update
更改df
的位置.
df = df.groupby(level='group').bfill()
df = df.unstack(0).bfill().stack().swaplevel(0, 1).reindex_like(df)
特定于列
df.Value = df.groupby(level=0).Value.bfill()
这篇关于 pandas 中的多索引fillna的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!