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?
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.
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.
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.
IIUC
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()
                        You can try this one:
df.keywords.map(len).sum()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With