Lumberjack88
Lumberjack88

Reputation: 15

Switch dataframe float numbers column into dataframe percentages column

This is my df_neweigenvalue pandas dataframe:

    Eigenvalues  Explained proportion
1      19.73283               0.98664
2       0.24447               0.01222
3       0.01922               0.00096
4       0.00277               0.00014
5       0.00063               0.00003
6       0.00008               0.00000
7       0.00001               0.00000
8       0.00000               0.00000
9       0.00000               0.00000
10      0.00000               0.00000
11      0.00000               0.00000
12      0.00000               0.00000
13      0.00000               0.00000
14      0.00000               0.00000
15      0.00000               0.00000
16      0.00000               0.00000
17      0.00000               0.00000
18      0.00000               0.00000
19      0.00000               0.00000
20      0.00000               0.00000

I tried to use this code to convert the second column into percentages but it doesn't seem to work:

df_neweigenvalue.style.format({'Explained proportion': '{:.2%}'})

The error I'm getting is this one:

pandas.io.formats.style.Styler object at 0x0000027446D79D90>

What am I doing wrong?

Upvotes: 0

Views: 95

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

IIUC:

Try:

df['proportion']=df['proportion'].mul(100).map('{:,.2f}%'.format)
#for whole dataframe use:
#df=df.mul(100).applymap('{:,.2f}%'.format)

OR

df['proportion']=df['proportion'].mul(100).round(2).astype(str)+'%'
#for whole dataframe use(numeric columns):
#df=df.mul(100).round(2).astype(str)+'%'

Upvotes: 1

Related Questions