Pandas percentage by value in a column

后端 未结 5 1937
死守一世寂寞
死守一世寂寞 2020-12-03 06:17

I want to get a percentage of a particular value in a df column. Say I have a df with (col1, col2 , col3, gender) gender column has values of M or F. I want to get the perc

5条回答
  •  天命终不由人
    2020-12-03 07:07

    finding the percentage of target variation to chenck imbalance/not.

    g = data[Target_col_Y]
    df = pd.concat([g.value_counts(),              
    g.value_counts(normalize=True).mul(100)],axis=1,keys=('counts','percentage'))
    
    print (df)
    

    counts percentage

    0 36548 88.734583

    1 4640 11.265417

    finding the maximum in the columns percentage here, to check how much #imbalance there

    df1=df.diff(periods=1,axis=0)
    difvalue=df1[[list(df1.columns)[-1]]].max()
    

提交回复
热议问题