Manak
Manak

Reputation: 47

Is there any SQL functionality to get this?

So I have a pandas dataframe in the following manner

     A  B  C
   1 X1 X3 X2

   2 X2 X1 X1

   3 X2 X3 X1

   4 X1 X1 X2

   5 X3 X2 X3

The output i want to display is

      A  B  C
  X1  2  2  2

  X2  2  1  2

  X3  1  2  1

How to use a pivot or groupby on this pandas df?

Upvotes: 0

Views: 33

Answers (1)

timgeb
timgeb

Reputation: 78760

Use

>>> df.apply(pd.Series.value_counts)
    A  B  C
X1  2  2  2
X2  2  1  2
X3  1  2  1

Upvotes: 4

Related Questions