pandas 如何在pandas python中创建频率表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40581312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a frequency table in pandas python
提问by Anup
If i have data like
如果我有这样的数据
Col1
A
B
A
B
A
C
I need output like
我需要像这样的输出
Col_value Count
A 3
B 2
C 1
I need to col_value and count be column names. So I can access it like a['col_value']
我需要 col_value 和 count 是列名。所以我可以像 ['col_value'] 一样访问它
回答by Boud
Use value_counts
:
使用value_counts
:
df = pd.value_counts(df.Col1).to_frame().reset_index()
df
A 3
B 2
C 1
then rename your columns if needed:
然后根据需要重命名您的列:
df.columns = ['Col_value','Count']
df
Col_value Count
0 A 3
1 B 2
2 C 1
回答by tippu_shaik
Use pd.crosstab
as another alternative:
使用pd.crosstab
的另一种选择:
import pandas as pd
help(pd.crosstab)
Help on function crosstab
in module pandas.core.reshape.pivot
:
crosstab
模块中的功能帮助pandas.core.reshape.pivot
:
crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)
Example:
例子:
df_freq = pd.crosstab(df['Col1'], columns='count')
df_freq.head()
回答by jezrael
回答by Alyssa Kelley
def frequencyTable(alist):
'''
list -> chart
Returns None. Side effect is printing two columns showing each number that
is in the list, and then a column indicating how many times it was in the list.
Example:
>>> frequencyTable([1, 3, 3, 2])
ITEM FREQUENCY
1 1
2 1
3 2
'''
countdict = {}
for item in alist:
if item in countdict:
countdict[item] = countdict[item] + 1
else:
countdict[item] = 1
itemlist = list(countdict.keys())
itemlist.sort()
print("ITEM", "FREQUENCY")
for item in itemlist:
print(item, " ", countdict[item])
return None