Reputation:
I would like to count the number of uppercase words in a pandas dataframe. I have a dataframe with a column in which there are lists of words in each cell. How can I do it ?
Upvotes: 2
Views: 1273
Reputation: 862911
You can need first flatten values of lists with DataFrame
constructor and stack
or numpy.concatenate
.
Then for separate word is necessary split
and stack
, last check words by isupper
and count by sum
:
df = pd.DataFrame({'A': [['a','GA'],['SA dsdf fds We','Da','ddb D']]})
print (df)
A
0 [a, GA]
1 [SA dsdf fds We, Da, ddb D]
b = pd.DataFrame(df['A'].values.tolist())
.stack()
.str.split(expand=True)
.stack()
.str.isupper()
.sum()
print (b)
3
Or:
b = pd.Series(np.concatenate(df['A'])).str.split(expand=True).stack().str.isupper().sum()
print (b)
3
Upvotes: 3