0

I want to calculate the count of how many names there are included in the the column of dataframe in Python.

I get the column names from dataframe as defined command

df["Names_Column"]

Here is the values of one column in dataframe

Names_Column
Damandeep Singh Baggan, Smita Malhotra, Baba Sehgal, Deepak Chachra
Damandeep Singh Baggan, Smita Malhotra, Deepak Chachra
...

I want to get a result of a count like this.

Name                     Count
Damandeep Singh Baggan     4
Deepak Chachra             3
Smita Malhotra             2
...

I can try to this code to seperate names but I couldn't do it.

separate = df["Names_Column"].str.split(",") 

How can I do it?

S.N
  • 2,157
  • 3
  • 29
  • 78
  • Providing a [mre] would be a good start, reading [ask] helps as well. – Patrick Artner Apr 19 '20 at 21:10
  • Hi Tony, the function that you are looking for is value_counts(); but I'm afraid that this question. is duplicated. you can find the answer in the following posts: 1) (https://stackoverflow.com/questions/15411158/pandas-countdistinct-equivalent)[https://stackoverflow.com/questions/15411158/pandas-countdistinct-equivalent] 2) (https://stackoverflow.com/questions/38309729/count-unique-values-with-pandas-per-groups)[https://stackoverflow.com/questions/38309729/count-unique-values-with-pandas-per-groups] – Shahar Apr 19 '20 at 21:19

1 Answers1

1

Combining explode, and value_counts on the column solves this problem.

import pandas as pd
df = pd.DataFrame([
['Damandeep Singh Baggan, Smita Malhotra, Baba Sehgal, Deepak Chachra'],
['Damandeep Singh Baggan, Smita Malhotra, Deepak Chachra']],columns=['Names_Column'])
df2 = df.apply(lambda x: x.str.split(', ').explode())
df2['Names_Column'].value_counts()

returns

                        count
Names_Column                 
Baba Sehgal                 1
Damandeep Singh Baggan      2
Deepak Chachra              2
Smita Malhotra              2
LudvigH
  • 3,662
  • 5
  • 31
  • 49