I get a dataframe as follows:
df
c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12
r1 0 1 1 1 1 0 0 0 0 0 0 0
r2 1 2 2 2 2 1 1 1 1 0 0 0
r3 1 0 2 0 0 1 0 0 0 0 0 0
func(df)
0 1 2
r1 8 4 0
r2 3 5 4
r3 9 2 1
And I want to do value_counts
on each row and get 3 columns which are corresponding to the count of each value.
What I thought is to do melt()
in each row, get values counts and fill it in corresponding place. BUT I think it is not a smart and convenient enough way.
So if not mind could anyone help me?
Thanks in advance.