Python – How to count all selected words equal to condition in a data frame?

How to count all selected words equal to condition in a data frame?… here is a solution to the problem.

How to count all selected words equal to condition in a data frame?

I have a data frame

where I want to count the number of words in a specific column in the whole data frame.

Suppose shape is a column in the data frame:

shape                             color
circle rectangle                  orange
square triangle 
rombus  

square oval                       black
triangle circle

rectangle oval                    white
triangle 

I want to count how many circles, rectangles, ellipses, triangles are in a data frame in a shape column.

The output should be:

circle    2
rectangle 2
triangle  3
oval      1

Solution

Use:

L = ['circle','rectangle','oval','triangle']

s = df['shape'].astype(str).str.split(expand=True).stack()
df = s[s.isin(L)].value_counts().reindex(L, fill_value=0).reset_index()
df.columns = ['vals','counts']
print (df)
        vals  counts
0     circle       2
1  rectangle       2
2       oval       2
3   triangle       3

Explanation:

  1. The first >split by space (default delimiter ) and >stack for Series Words
  2. Press isin filter by list
  3. Used to count value_counts
  4. If necessary, change the order or add missing values using 0 Add reindex
  5. Add for DataFrame in Series reset_index

Related Problems and Solutions