I need to count integers 1 to 5 row-wise in a Pandas dataframe. For example, for
import pandas as pd
df = pd.DataFrame({'c1': [3, 1, 2], 'c2': [3, 3, 3], 'c3': [2, 5, None], 'c4': [1, 2, 3]})
c1 c2 c3 c4
0 3 3 2.0 1
1 1 3 5.0 2
2 2 3 NaN 3
The following would be created:
n1 n2 n3 n4 n5
0 1 1 2 0 0
1 1 1 1 0 1
2 0 1 2 0 0
I've come across .value_counts
and crosstab
, but I just haven't been able to set up either to get what I need. Any help would be much appreciated.
Thanks in advance!