【发布时间】:2018-01-29 15:23:15
【问题描述】:
我有以下数据框:
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
df.loc["B", (slice(None), 'two')]=np.nan
现在,我想向前填充“baz”和“foo”列的数据(而不是“bar”和“qux”列)。我试过了:
df[["baz", "foo"]].ffill(inplace=True)
但生成的数据框没有向前填充任何值。 如何仅为这两列创建包含前向填充数据的数据框?
【问题讨论】:
-
在 v0.20 上为我工作。你用的是什么版本?
-
这里一样,0.20 pandas 效果很好
-
我使用的是 0.20.3 版本。该函数没有报错,但是如果我打印df,NaN数据仍然存在。
标签: python pandas dataframe hierarchical fillna