Python Forum

Full Version: pandas: Compute the % of the unique values in a column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I am new to crosstab & pivot tables.


DF


|   |  Firm |
0  |  A      | 
1  |  A      | 
2  |  B      | 
3  |  B      | 

Desired Outcome



A | 0.5
B |  0.5
Tried the below but get
TypeError: crosstab() missing 1 required positional argument: 'columns'

ct_df = pd.crosstab(df['Firm'], normalize=True, margins=True, dropna=False)
For that would use groupby, not crosstab.

import pandas as pd
df = pd.DataFrame([[1,'A'],[2,'A'],[3,'B'],[4,'B']], columns=['Numbers','Letters'])
idx = df.index
nrows = len(idx)
df1 = df.groupby(by='Letters').size()/nrows
df1
Output:
Letters A 0.5 B 0.5 dtype: float64