Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count a pandas dataframe over duplications

My initial dataframe is:

    Name        Info1        Info2
0  Name1  Name1-Info1  Name1-Info2
1  Name1  Name1-Info1  Name1-Info2
2  Name1  Name1-Info1  Name1-Info2
3  Name2  Name2-Info1  Name2-Info2
4  Name2  Name2-Info1  Name2-Info2

and i would like to return the number of repetitions of each row as such:

    Name        Info1        Info2  Count
0  Name1  Name1-Info1  Name1-Info2      3
1  Name2  Name2-Info1  Name2-Info2      2

How can I count a pandas dataframe over duplications?

like image 959
toby chamberlain Avatar asked Aug 03 '20 11:08

toby chamberlain


People also ask

How do you check if there are duplicates in pandas DataFrame?

Finding duplicate rows To take a look at the duplication in the DataFrame as a whole, just call the duplicated() method on the DataFrame. It outputs True if an entire row is identical to a previous row.

How do you count occurrences of pandas?

How do you Count the Number of Occurrences in a data frame? To count the number of occurrences in e.g. a column in a dataframe you can use Pandas value_counts() method. For example, if you type df['condition']. value_counts() you will get the frequency of each unique value in the column “condition”.


1 Answers

df.groupby(['Name', 'Info1', 'Info2']).size().reset_index().rename(columns={0:"count"})
like image 80
Tom Ron Avatar answered Sep 22 '22 00:09

Tom Ron