Pandas groupby on a column of lists

前端 未结 1 1310
遥遥无期
遥遥无期 2020-12-21 17:38

I have a pandas dataframe with a column that contains lists:

df = pd.DataFrame({\'List\': [[\'once\', \'upon\'], [\'once\', \'upon\         


        
相关标签:
1条回答
  • 2020-12-21 18:24

    One way is to convert to tuples first. This is because pandas.groupby requires keys to be hashable. Tuples are immutable and hashable, but lists are not.

    res = df.groupby(df['List'].map(tuple))['Count'].sum()
    

    Result:

    List
    (a, time)       6
    (once, upon)    5
    (there, was)    1
    Name: Count, dtype: int64
    

    If you need the result as lists in a dataframe, you can convert back:

    res = df.groupby(df['List'].map(tuple))['Count'].sum()
    res['List'] = res['List'].map(list)
    
    #            List  Count
    # 0     [a, time]      6
    # 1  [once, upon]      5
    # 2  [there, was]      1
    
    0 讨论(0)
提交回复
热议问题