g1这是一个
Dataframe。但是,它具有层次结构索引:
In [19]: type(g1)Out[19]: pandas.core.frame.DataframeIn [20]: g1.indexOut[20]: MultiIndex([('Alice', 'Seattle'), ('Bob', 'Seattle'), ('Mallory', 'Portland'), ('Mallory', 'Seattle')], dtype=object)也许你想要这样的东西?
In [21]: g1.add_suffix('_Count').reset_index()Out[21]: Name City City_Count Name_Count0 Alice Seattle111 Bob Seattle222 Mallory Portland223 Mallory Seattle11或类似的东西:
In [36]: Dataframe({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()Out[36]: Name City count0 Alice Seattle 11 Bob Seattle 22 Mallory Portland 23 Mallory Seattle 1


