I have seen several posts giving solutions to counting frequencies of either numbers or categories by column in a pandas data frame, such as this. If I have a data frame n x m having only numeric, I want to do the same for the entire data frame, how do I do that without first flattening it into one column/vector? Thanks,
Asked
Active
Viewed 120 times
0
-
Have you tried `.value_counts()` on the whole dataframe? Like `df.value_counts()` – Yevhen Kuzmovych Feb 06 '21 at 14:23
-
@YevhenKuzmovych Hi, yes, but it counts the occurence of the entire row, not each number. – Tristan Tran Feb 06 '21 at 14:24
1 Answers
1
You can use numpy
for this:
values, counts = np.unique(df, return_counts=True)
for v, c in zip(values, counts):
print(v, c)
for dataframe like
a b
0 1 2
1 1 3
it will output
1 2
2 1
3 1

Yevhen Kuzmovych
- 10,940
- 7
- 28
- 48