Pandas groupby with dict

后端 未结 2 1092
无人共我
无人共我 2020-12-18 09:08

Is it possible to use a dict to group on elements of a column?

For example:

In [3]: df = pd.DataFrame({\'A\' : [\'one\', \'one\', \'two\', \'three\'         


        
2条回答
  •  旧时难觅i
    2020-12-18 10:06

    You can group with a dictionary, but (as with any group by operation) you need to set the index column first.

    grouped = df.set_index("A").groupby(d)
    
    list(grouped)
    # [('End',               B
    # A              
    # three -1.550727
    # three  1.048730
    # 
    # [2 rows x 1 columns]), ('Start',             B
    # A            
    # one -1.552152
    # one -2.018647
    # two -0.968068
    # two  0.449016
    # two -0.374453
    # one  0.116770
    # 
    # [6 rows x 1 columns])]
    

提交回复
热议问题