[英]Count the frequency that a value occurs in a dataframe column
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
Use groupby
and count
:使用
groupby
和count
:
In [37]:
df = pd.DataFrame({'a':list('abssbab')})
df.groupby('a').count()
Out[37]:
a
a
a 2
b 3
s 2
[3 rows x 1 columns]
See the online docs: https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html查看在线文档: https : //pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html
Also value_counts()
as @DSM has commented, many ways to skin a cat here还有
value_counts()
正如@DSM 所评论的,这里有很多给猫剥皮的方法
In [38]:
df['a'].value_counts()
Out[38]:
b 3
a 2
s 2
dtype: int64
If you wanted to add frequency back to the original dataframe use transform
to return an aligned index:如果您想将频率添加回原始数据帧,请使用
transform
返回对齐的索引:
In [41]:
df['freq'] = df.groupby('a')['a'].transform('count')
df
Out[41]:
a freq
0 a 2
1 b 3
2 s 2
3 s 2
4 b 3
5 a 2
6 b 3
[7 rows x 2 columns]
If you want to apply to all columns you can use:如果要应用于所有列,可以使用:
df.apply(pd.value_counts)
This will apply a column based aggregation function (in this case value_counts) to each of the columns.这会将基于列的聚合函数(在本例中为 value_counts)应用于每一列。
df.category.value_counts()
This short little line of code will give you the output you want.这短短的一小行代码将为您提供所需的输出。
If your column name has spaces you can use如果您的列名有空格,您可以使用
df['category'].value_counts()
df.apply(pd.value_counts).fillna(0)
value_counts - Returns object containing counts of unique values value_counts - 返回包含唯一值计数的对象
apply - count frequency in every column. apply - 计算每列中的频率。 If you set
axis=1
, you get frequency in every row如果你设置
axis=1
,你会得到每一行的频率
fillna(0) - make output more fancy. fillna(0) - 使输出更花哨。 Changed NaN to 0
将 NaN 更改为 0
In 0.18.1 groupby
together with count
does not give the frequency of unique values:在 0.18.1
groupby
和count
一起没有给出唯一值的频率:
>>> df
a
0 a
1 b
2 s
3 s
4 b
5 a
6 b
>>> df.groupby('a').count()
Empty DataFrame
Columns: []
Index: [a, b, s]
However, the unique values and their frequencies are easily determined using size
:但是,可以使用
size
轻松确定唯一值及其频率:
>>> df.groupby('a').size()
a
a 2
b 3
s 2
With df.a.value_counts()
sorted values (in descending order, ie largest value first) are returned by default.使用
df.a.value_counts()
排序值(按降序排列,即最大值在前)默认返回。
Using list comprehension and value_counts for multiple columns in a df对 df 中的多列使用列表理解和 value_counts
[my_series[c].value_counts() for c in list(my_series.select_dtypes(include=['O']).columns)]
https://stackoverflow.com/a/28192263/786326 https://stackoverflow.com/a/28192263/786326
If your DataFrame has values with the same type, you can also set return_counts=True
in numpy.unique() .如果您的 DataFrame 具有相同类型的值,您还可以在numpy.unique() 中设置
return_counts=True
。
index, counts = np.unique(df.values,return_counts=True)
np.bincount() could be faster if your values are integers.如果您的值是整数, np.bincount()可能会更快。
Without any libraries, you could do this instead:没有任何库,你可以这样做:
def to_frequency_table(data):
frequencytable = {}
for key in data:
if key in frequencytable:
frequencytable[key] += 1
else:
frequencytable[key] = 1
return frequencytable
Example:例子:
to_frequency_table([1,1,1,1,2,3,4,4])
>>> {1: 4, 2: 1, 3: 1, 4: 2}
You can also do this with pandas by broadcasting your columns as categories first, eg dtype="category"
eg您也可以通过首先将您的列作为类别广播来对
dtype="category"
执行此操作,例如dtype="category"
例如
cats = ['client', 'hotel', 'currency', 'ota', 'user_country']
df[cats] = df[cats].astype('category')
and then calling describe
:然后调用
describe
:
df[cats].describe()
This will give you a nice table of value counts and a bit more :):这将为您提供一个很好的值计数表和更多:):
client hotel currency ota user_country
count 852845 852845 852845 852845 852845
unique 2554 17477 132 14 219
top 2198 13202 USD Hades US
freq 102562 8847 516500 242734 340992
@metatoaster has already pointed this out. @metatoaster 已经指出了这一点。 Go for
Counter
.去
Counter
。 It's blazing fast.它的速度很快。
import pandas as pd
from collections import Counter
import timeit
import numpy as np
df = pd.DataFrame(np.random.randint(1, 10000, (100, 2)), columns=["NumA", "NumB"])
%timeit -n 10000 df['NumA'].value_counts()
# 10000 loops, best of 3: 715 µs per loop
%timeit -n 10000 df['NumA'].value_counts().to_dict()
# 10000 loops, best of 3: 796 µs per loop
%timeit -n 10000 Counter(df['NumA'])
# 10000 loops, best of 3: 74 µs per loop
%timeit -n 10000 df.groupby(['NumA']).count()
# 10000 loops, best of 3: 1.29 ms per loop
Cheers!干杯!
I believe this should work fine for any DataFrame columns list.我相信这适用于任何 DataFrame 列列表。
def column_list(x):
column_list_df = []
for col_name in x.columns:
y = col_name, len(x[col_name].unique())
column_list_df.append(y)
return pd.DataFrame(column_list_df)
column_list_df.rename(columns={0: "Feature", 1: "Value_count"})
The function "column_list" checks the columns names and then checks the uniqueness of each column values.函数“column_list”检查列名称,然后检查每个列值的唯一性。
The following code creates frequency table for the various values in a column called "Total_score" in a dataframe called "smaller_dat1", and then returns the number of times the value "300" appears in the column.以下代码为名为“smaller_dat1”的数据帧中名为“Total_score”的列中的各种值创建频率表,然后返回值“300”在该列中出现的次数。
valuec = smaller_dat1.Total_score.value_counts()
valuec.loc[300]
n_values = data.income.value_counts()
First unique value count第一个唯一值计数
n_at_most_50k = n_values[0]
Second unique value count第二个唯一值计数
n_greater_50k = n_values[1]
n_values
Output:输出:
<=50K 34014
>50K 11208
Name: income, dtype: int64
Output:输出:
n_greater_50k,n_at_most_50k:-
(11208, 34014)
Use this code: 使用此代码:
import numpy as np
np.unique(df['a'],return_counts=True)
your data:
|category|
cat a
cat b
cat a
solution:解决方案:
df['freq'] = df.groupby('category')['category'].transform('count')
df = df.drop_duplicates()
As everyone said, the faster solution is to do:正如大家所说,更快的解决方案是:
df.column_to_analyze.value_counts()
But if you want to use the output in your dataframe, with this schema:但是,如果您想在数据框中使用输出,请使用以下架构:
df input:
category
cat a
cat b
cat a
df output:
category counts
cat a 2
cat b 1
cat a 2
you can do this:你可以这样做:
df['counts'] = df.category.map(df.category.value_counts())
df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.