1

I need to count the occurrence of entire row in pandas DataFrame

For example if I have the Data Frame:

A = pd.DataFrame([['a','b','c'],['b','a','c'],['a','b','c']])

The expected result should be:

'a','b','c' : 2

'b','a','c' : 1

value_counts only counts the occurrence of one element in a series (one column)

I can create new column that will have all the element of that row and count values in that column, but I hope for a better solution.

R_Dax
  • 706
  • 3
  • 10
  • 25
tumir
  • 39
  • 1
  • 7
  • Please provide an [mcve] to show what you have tried so far and where you are stuck at the moment. In addition, you might want to have a look at this answer and _adopt_ it to your needs: https://stackoverflow.com/questions/15943769/how-do-i-get-the-row-count-of-a-pandas-dataframe – albert Aug 03 '21 at 07:17
  • thank. I want to count occurrent of specific row, not all rows in Data Frame. I edited my question – tumir Aug 03 '21 at 07:25

2 Answers2

0

You can do this:

A = pd.DataFrame([['a','b','c'],['b','a','c'],['a','b','c']])
A.groupby(A.columns.tolist(),as_index=False).size()

which returns:

 0  1  2  size
0  a  b  c     2
1  b  a  c     1

0

Below code would provide the result you are expecting.

df = pd.DataFrame([['a','b','c'],['b','a','c'],['a','b','c']])

df.groupby(list(df.columns))[0].count().to_frame('count').reset_index()

above code results in following dataframe.

    0   1   2   count

 0  a   b   c   2

 1  b   a   c   1
R_Dax
  • 706
  • 3
  • 10
  • 25
  • that is a good solution for that specific example, but I got the base idea. groupby for all needed columns (in my case all columns) and counting the size of each group. thanks – tumir Aug 03 '21 at 08:11