I have a dataframe with one row:
0 1 2 3 4 5 6 7 8
--------------------
0 4 2 4 1 3 2 1 1 3
I need for the code to count the number of same values in the row. So the output is:
1 = 3
2 = 2
3 = 2
4 = 2
CodePudding user response:
Use Series value_counts()
method:
df.iloc[0, :].value_counts()
CodePudding user response:
transpose it (df.T) then use df.value_counts().
df.T.value_counts()
CodePudding user response:
Transform the column into a list and then:
df = pd.DataFrame([0 ,4 ,2 ,4, 1, 3, 2, 1, 1, 3])
print([{i:list(df[0]).count(i)} for i in range(max(list(df[0])) 1)])
Output:
[{0: 1}, {1: 3}, {2: 2}, {3: 2}, {4: 2}]