简体   繁体   中英

How to do value counts with groupby in a numpy array pandas column

I have a pandas dataframe like this:

import pandas as pd
foo = pd.DataFrame({'s': ['a','b','a','a'], 'versions':[['1','2'],'2',['1','2'],'2']})

I would like to value_counts() he columns versions by group s

I have tried

foo.groupby('s')['versions'].value_counts()

but i get an error TypeError: unhashable type: 'list' , any ideas?

You can convert lists/arrays to hashable tuples, but because there are also strings need if-else statement for avoid split strings 100 to tuple (1,0,0) :

foo['versions'] = foo['versions'].apply(lambda x: tuple(x) if isinstance(x, list) else (x,))
print (foo)
   s versions
0  a   (1, 2)
1  b     (2,)
2  a   (1, 2)
3  a     (2,)

s = foo.groupby('s')['versions'].value_counts()
print (s)
s  versions
a  (1, 2)      2
   (2,)        1
b  (2,)        1
Name: versions, dtype: int64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM