Converting a Pandas GroupBy output from Series to DataFrame

后端 未结 9 749
广开言路
广开言路 2020-11-22 09:58

I\'m starting with input data like this

df1 = pandas.DataFrame( { 
    \"Name\" : [\"Alice\", \"Bob\", \"Mallory\", \"Mallory\", \"Bob\" , \"Mallory\"] , 
           


        
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 10:34

    g1 here is a DataFrame. It has a hierarchical index, though:

    In [19]: type(g1)
    Out[19]: pandas.core.frame.DataFrame
    
    In [20]: g1.index
    Out[20]: 
    MultiIndex([('Alice', 'Seattle'), ('Bob', 'Seattle'), ('Mallory', 'Portland'),
           ('Mallory', 'Seattle')], dtype=object)
    

    Perhaps you want something like this?

    In [21]: g1.add_suffix('_Count').reset_index()
    Out[21]: 
          Name      City  City_Count  Name_Count
    0    Alice   Seattle           1           1
    1      Bob   Seattle           2           2
    2  Mallory  Portland           2           2
    3  Mallory   Seattle           1           1
    

    Or something like:

    In [36]: DataFrame({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()
    Out[36]: 
          Name      City  count
    0    Alice   Seattle      1
    1      Bob   Seattle      2
    2  Mallory  Portland      2
    3  Mallory   Seattle      1
    

提交回复
热议问题