Home > Software design >  count the number of appearance of a string in each column of a dataframe
count the number of appearance of a string in each column of a dataframe

Time:09-22

I have a dataframe with the string "name" appearing and I want to count the appearance of it for each column. I would like the results in a new df.

import pandas as pd

data1 = {'first_column':  ['1', '2', 'name'],
        'second_column': ['name', 'name', 'name'],
         'id_number':['name', '4', 'name'],
        'fourth_column':['5', 'name', '2'],
        }


df1 = pd.DataFrame(data1)

enter image description here

I tried this but this gave me this output but I don't have a new df and have too many results:

for col in df1:
  df2 = df1[df1[col] == "name"].count()
  print(df2)

enter image description here

CodePudding user response:

You can check for equality for the entire dataframe, then call sum on it, and since you need it as a dataframe, you can call to_frame at last:

>>> df1.eq('name').sum().to_frame('count')

               count
first_column       1
second_column      3
id_number          2
fourth_column      1
  • Related