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
Without any libraries, you could do this instead:
Example:
Use size() method:
Using list comprehension and value_counts for multiple columns in a df
https://stackoverflow.com/a/28192263/786326
Use
groupby
andcount
:See the online docs: http://pandas.pydata.org/pandas-docs/stable/groupby.html
Also
value_counts()
as @DSM has commented, many ways to skin a cat hereIf you wanted to add frequency back to the original dataframe use
transform
to return an aligned index:In 0.18.1
groupby
together withcount
does not give the frequency of unique values:However, the unique values and their frequencies are easily determined using
size
:With
df.a.value_counts()
sorted values (in descending order, i.e. largest value first) are returned by default.You can also do this with pandas by broadcasting your columns as categories first, e.g.
dtype="category"
e.g.and then calling
describe
:This will give you a nice table of value counts and a bit more :):