Python Forum
pandas: Compute the % of the unique values in a column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas: Compute the % of the unique values in a column
#1
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)
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Make unique id in vectorized way based on text data column with similarity scoring ill8 0 341 Dec-12-2022, 03:22 AM
Last Post: ill8
  Increase df column values decimals SriRajesh 2 564 Nov-14-2022, 05:20 PM
Last Post: deanhystad
  pandas column percentile nuncio 7 1,183 Aug-10-2022, 04:41 AM
Last Post: nuncio
  how to expand each unique value in another column and fill zero if no match SriRajesh 0 417 Jul-10-2022, 09:21 AM
Last Post: SriRajesh
  Separating unique, stable, samples using pandas keithpfio 1 529 Jun-20-2022, 07:06 PM
Last Post: keithpfio
  update values in one dataframe based on another dataframe - Pandas iliasb 2 6,611 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 1,945 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  How to move each team row to a new column. Pandas vladiwnl 0 1,362 Jun-13-2021, 08:10 AM
Last Post: vladiwnl
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 2,603 May-09-2021, 06:46 PM
Last Post: Pit292
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,310 Jan-11-2021, 09:53 PM
Last Post: Azureaus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020