我有这样的df:
Allotments SH_Class SH_Percent
A. Annex BNW 16.187500
A. Annex MTGP 1.0000000
A. Annex Meadow 39.687500
A. Annex PHP 29.687500
A. Annex SP 16.250000
A. Annex WMTGP 10.833333
A. Annex Woodland 5.000000
Arnstson Meadow 28.750000
Arnstson PHP 61.590909
Arnstson SP 18.125000
Arnstson WMTGP 2.500000
Arnstson Woodland 17.083333
我希望每个唯一的
SH_Class
的Allotments
都相同。因此,即使Arnstson
中没有MTGP
或BNW
,我也希望它添加一个相应的SH_Percent
为零。我想要的输出是: Allotments SH_Class SH_Percent
A. Annex BNW 16.187500
A. Annex MTGP 1.0000000
A. Annex Meadow 39.687500
A. Annex PHP 29.687500
A. Annex SP 16.250000
A. Annex WMTGP 10.833333
A. Annex Woodland 5.000000
Arnstson BNW 0.0000000
Arnstson MTGP 0.0000000
Arnstson Meadow 28.750000
Arnstson PHP 61.590909
Arnstson SP 18.125000
Arnstson WMTGP 2.500000
Arnstson Woodland 17.083333
我已经试过这段代码:
target_ix = pd.MultiIndex.from_product([df.Allotments.unique(),
levels='BNW', 'MTGP', 'Meadow', 'PHP', 'SP', 'WMTGP', 'Woodland'])
new_df = df.loc[target_ix]
但它不太正确。
最佳答案
你近了...
idx = pd.MultiIndex.from_product([df.Allotments.unique(), df.SH_Class.unique()],
names=['Allotments', 'SH_Class'])
>>> df.set_index(['Allotments', 'SH_Class']).ix[idx].fillna(0).reset_index()
Allotments SH_Class SH_Percent
0 A. Annex BNW 16.187500
1 A. Annex MTGP 1.000000
2 A. Annex Meadow 39.687500
3 A. Annex PHP 29.687500
4 A. Annex SP 16.250000
5 A. Annex WMTGP 10.833333
6 A. Annex Woodland 5.000000
7 Arnstson BNW 0.000000
8 Arnstson MTGP 0.000000
9 Arnstson Meadow 28.750000
10 Arnstson PHP 61.590909
11 Arnstson SP 18.125000
12 Arnstson WMTGP 2.500000
13 Arnstson Woodland 17.083333
关于python - MultIindex填充数据框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35140235/