I have a dataset
|category|
cat a
cat b
cat a
I'd like to be able to return something like (showing unique values and frequency)
category | freq |
cat a 2
cat b 1
I have a dataset
|category|
cat a
cat b
cat a
I'd like to be able to return something like (showing unique values and frequency)
category | freq |
cat a 2
cat b 1
If your DataFrame has values with the same type, you can also set
return_counts=True
in numpy.unique().index, counts = np.unique(df.values,return_counts=True)
np.bincount() could be faster if your values are integers.
This should work:
@metatoaster has already pointed this out. Go for
Counter
. It's blazing fast.Timers
Cheers!
If you want to apply to all columns you can use:
This will apply a column based aggregation function (in this case value_counts) to each of the columns.
First unique value count
Second unique value count
Output:
Output:
Assuming you have a Pandas Dataframe
df
, try:The Pandas Manual provides more information.