简体   繁体   中英

Count the occurrence of each value in a Pandas column in a separate list

I have a Pandas dataframe column with unique values:

  c1
1 a
2 b
3 c

I want to count the occurrences of these values in two separate lists, and append the counts to the dataframe. The separate list might look like:

my_list1 = [a,b,a,c,c,a,b,a]
my_list2 = [b,c,c,a,a,b,c,a]

So the final result would be a pandas dataframe similar to this:

   c1 c2 c3
1  a  4  3
2  b  2  2
3  c  2  3

I could make the list into a dictionary and create a dataframe from that dictionary, but this is complicated by needing to do this for two lists. I tried to make a dictionary of the two dictionaries, but could not get it into a dataframe.

Use collections.Counter()

from collections import Counter

df['c2'] = df['c1'].map(Counter(my_list1))
df['c3'] = df['c1'].map(Counter(my_list2))
# print(df)

  c1  c2  c3
0  a   4   3
1  b   2   2
2  c   2   3

You can also use python's built-in count() for lists, without having to import collections.Counter :

df['c2'] = df.c1.map(my_list1.count)
df['c3'] = df.c1.map(my_list2.count)

#   c1  c2  c3
# 1  a   4   3
# 2  b   2   2
# 3  c   2   3

Explanation

When Series.map() is given a function in this way, it feeds each value of the Series to the function and assigns the mapped output.

So this:

df.c1.map(my_list1.count)

Is doing this:

my_list1.count('a')
my_list1.count('b')
my_list1.count('c')

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