简体   繁体   中英

python pandas count the number of occurances inside lists in a column

Say I have a dataframe like

    A     B     
0  [1,2]  Q     

1  [1,3]  Q     

2  [4,2]  Q     

I want the count of each element inside the lists in the column A

So the result I want is

    Number  Count
0     1       2

1     2       2

2     3       1

3     4       1

I am not sure how to do it. Thank you in advance

Convert the series of list to a dataframe, stack the columns and use value_counts on that series ie

count = pd.DataFrame(df['A'].values.tolist()).stack().value_counts()

pd.DataFrame({'Count':count.values,'Number':count.index})

  Count  Number
0      2       2
1      2       1
2      1       4
3      1       3

# demo dataframe : df = pd.DataFrame({'A': [[1,2], [1,3], [4,2]]})

You need:

from collections import Counter
sl = []
_ = [sl.extend(j) for i,j in df['A'].items()]
x = Counter(sl)

new_df = pd.DataFrame({'Number': list(x.keys()), 'Count': list(x.values())})
print(new_df)

Output

    Number  Count
0   1       2
1   2       2
2   3       1
3   4       1

You can use collections.Counter with itertools.chain :

from itertools import chain
from collections import Counter

counts = Counter(chain.from_iterable(df['A']))

res = pd.DataFrame.from_dict(counts, orient='index').reset_index()
res.columns =['Number', 'Count']

print(res)

   Number  Count
0       1      2
1       2      2
2       3      1
3       4      1

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