Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count total number of list elements in pandas column

I have a pandas dataframe A with column keywords as (here Im showing only 4 rows but in actual there are millions) :-

 keywords
 ['loans','mercedez','bugatti']
 ['trump','usa']
 ['galaxy','7s','canon','macbook']
 ['beiber','spiderman','marvels','ironmen']

I want to sum total number of list elements in column keywords and store it into some variable. Something like

total_sum=elements in keywords[0]+elements in keywords[1]+elements in 
          keywords[2]+elements in keywords[3]

total_sum=3+2+4+4
total_sum=13

How I can do it in pandas?

like image 681
Learner Avatar asked Sep 09 '18 18:09

Learner


People also ask

How do I count the number of items in a column in pandas?

We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.

How do you find the length of a list in a DataFrame?

Len() Method There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.

How do you count in pandas series?

Pandas: Series - count() functionThe count() function is used to get number of non-NA/null observations in the Series. If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a smaller Series. Number of non-null values in the Series.


2 Answers

IIUC

Setup

df = pd.DataFrame()
df['keywords']=[['loans','mercedez','bugatti'], 
                ['trump','usa'], 
                ['galaxy','7s','canon','macbook'], 
                ['beiber','spiderman','marvels','ironmen']]

Then juse use str.len and sum

df.keywords.str.len().sum()

Detail:

df.keywords.str.len()

0    3
1    2
2    4
3    4
Name: keywords, dtype: int64

Ps: If you have strings that look like a list, use ast.literal_eval to convert to list first.

df.keywords.transform(ast.literal_eval).str.len().sum()
like image 153
rafaelc Avatar answered Sep 20 '22 21:09

rafaelc


You can try this one:

df.keywords.map(len).sum()
like image 27
merenptah Avatar answered Sep 21 '22 21:09

merenptah