Skip to content
Advertisement

Count frequencies (unique rows) from a pandas list type column

I have a dataframe (df) like this:

JavaScript

And, I have list like this:

JavaScript

For each element in l, I want to count the unique rows they appear in df.

JavaScript

But I’m not getting the part where I can check if the value exists in the list-column of the dataframe.

JavaScript

Anyway I can fix this? Or is there a more cleaner/efficient way?

N.B. There is a similar question Frequency counts for a pandas column of lists, but it is different as I have an external list to check the frequency.

Advertisement

Answer

Here we are using apply method that applies given function to each element of the column (in our case it is the function that tests whether an element belongs to the list or not), then we sum True values, i.e. rows in which we found requested values and eventually save it to the dictionary. And we do it for all requested letters. I have not tested performance of this solution.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement