pandas: Compute the % of the unique values in a column - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: pandas: Compute the % of the unique values in a column (/thread-35356.html) |
pandas: Compute the % of the unique values in a column - JaneTan - Oct-23-2021 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.5Tried the below but get TypeError: crosstab() missing 1 required positional argument: 'columns' ct_df = pd.crosstab(df['Firm'], normalize=True, margins=True, dropna=False) RE: pandas: Compute the % of the unique values in a column - jefsummers - Oct-25-2021 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
|