Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a frequency table in pandas python

Tags:

python

pandas

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']

like image 278
Anup Avatar asked Nov 14 '16 03:11

Anup


3 Answers

Use 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
like image 52
Zeugma Avatar answered Oct 05 '22 01:10

Zeugma


Another solution is groupby with aggregating 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
like image 32
jezrael Avatar answered Oct 05 '22 00:10

jezrael


Use pd.crosstab as another alternative:

import pandas as pd

help(pd.crosstab)

Help on function crosstab in module 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()
like image 27
tippu_shaik Avatar answered Oct 05 '22 00:10

tippu_shaik