逐行填充panda空白,不重置索引



嗨,在分组依据:之后,我有一个这样的表

t = df.loc[(year-3 <= year) & (year <= year-1), 'Net Sum'].groupby([month, association]).sum()

t
YearMonth  Type
1          Other                  27471.73
base               -14563752.74
plan                16286620.30
2          Other                 754691.36
base                30465722.53
plan                17906687.29
3          Other                  20285.92
base                29339325.21
plan                15492558.91

我如何在不重置索引的情况下用分组的年月来填充空格,因为我想将年月作为索引?

预期结果。

t
YearMonth  Type
1          Other                  27471.73
1          base               -14563752.74
1          plan                16286620.30
2          Other                 754691.36
2          base                30465722.53
2          plan                17906687.29
3          Other                  20285.92
3          base                29339325.21
3          plan                15492558.91

我认为这只能通过更改显示选项来实现:

with pd.option_context('display.multi_sparse', False):
print(t)

如果我们参考文件

display.multi_parse True"稀疏"多索引显示(不显示组内外层的重复元素(

因此,我们可以将其设置为False。

以下应该做的工作

t.reset_index((

https://pandas.pydata.org/pandasdocs/stable/reference/api/pandas.DataFrame.reset_index.html

最新更新