Python 如何在作为特征唯一值的层次索引中获取数据帧
我得到一个Python 如何在作为特征唯一值的层次索引中获取数据帧,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我得到一个df,如下所示: tradeMonth tradeDay tradeMoney plate 0 12.0 2 2000.0 BK00052 1 11.0 1 2000.0 BK00052 2 2.0 0 16000.0 BK00028 3 4.0 1 1600.0 BK0002
df
,如下所示:
tradeMonth tradeDay tradeMoney plate
0 12.0 2 2000.0 BK00052
1 11.0 1 2000.0 BK00052
2 2.0 0 16000.0 BK00028
3 4.0 1 1600.0 BK00028
4 2.0 2 2900.0 BK00017
plate tradeMonth tradeDay tradeMoney
BK00052 12.0 2 2000.0
11.0 1 2000.0
BK00028 2.0 0 16000.0
4.0 1 1600.0
BK00017 2.0 2 2900.0
我想使用groupby('plate')
获得df
,如下所示:
tradeMonth tradeDay tradeMoney plate
0 12.0 2 2000.0 BK00052
1 11.0 1 2000.0 BK00052
2 2.0 0 16000.0 BK00028
3 4.0 1 1600.0 BK00028
4 2.0 2 2900.0 BK00017
plate tradeMonth tradeDay tradeMoney
BK00052 12.0 2 2000.0
11.0 1 2000.0
BK00028 2.0 0 16000.0
4.0 1 1600.0
BK00017 2.0 2 2900.0
我尝试使用df.unique()
来获取plate
的唯一值作为层次索引。但是我接下来应该实现什么呢
有人能帮我吗?谢谢你的帮助。很简单
df = df.groupby('plate').groups
print df
您不需要在此处提及unique。如果通过以下方式创建多索引:
第一级中的数据似乎是唯一的,但不是,只是默认情况下不显示。对于验证,请将
multi_sparse
更改为False
:
我们已经“稀疏化”了更高级别的索引,以使控制台输出看起来更容易。请注意,可以使用多稀疏选项控制索引的显示方式: