如何在pandas python中创建频率表

时间:2016-11-14 03:04:30

标签: python pandas

如果我有

这样的数据
Col1 
A
B
A
B
A
C

我需要输出

Col_value          Count
A                  3   
B                  2
C                  1

我需要col_value并计算列名。 所以我可以像['col_value']

那样访问它

4 个答案:

答案 0 :(得分:4)

使用value_counts

df = pd.value_counts(df.Col1).to_frame().reset_index()
df
A    3
B    2
C    1

然后根据需要重命名您的列:

df.columns = ['Col_value','Count']

df
  Col_value  Count
0         A      3
1         B      2
2         C      1

答案 1 :(得分:1)

另一个解决方案是groupby,汇总size

df = df.groupby('Col1')
       .size()
       .reset_index(name='Count')
       .rename(columns={'Col1':'Col_value'})
print (df)
  Col_value  Count
0         A      3
1         B      2
2         C      1

答案 2 :(得分:1)

使用pd.crosstab作为另一种选择:

import pandas as pd

help(pd.crosstab)

模块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)

示例:

df_freq = pd.crosstab(df['Col1'], columns='count')

df_freq.head()

答案 3 :(得分:0)

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