Home > Software engineering >  Get the percentage of Value dtypes present in a column of dataframe
Get the percentage of Value dtypes present in a column of dataframe

Time:01-19

I have a pandas dataframe, I need a bifurcation of value present in column of pandas dataframe,

data = {
       "A": [420, 380.2, 390.5,"aplha","beta","gamma"],
       "B": [520.30, 380.2, 390.5,885.07,56.3,45.2],
       }

in above dataframe in column "A" there are different type of values.

Required output

I need a output of data bifurcation of dtypes of value in column "A"

i.e. Column "A" has

--> 1 int value i.e (420),

--> 2 float values i.e (380.2, 390.5),

--> 3 String values i.e ("aplha","beta","gamma")

I need values in percentage of every value. Column has 50% string value, other 50% is int64 and float64 values

Where as in Column "B" all values are in float i.e 100%

CodePudding user response:

For percentages by types per all columns use:

df1 = df.applymap(type).apply(pd.value_counts, normalize=True).mul(100).fillna(0)
print (df1)
                         A      B
<class 'str'>    50.000000    0.0
<class 'float'>  33.333333  100.0
<class 'int'>    16.666667    0.0
  • Related