Home > Enterprise >  How to format a float percent pandas dataframe column into '{:,.2f%}'
How to format a float percent pandas dataframe column into '{:,.2f%}'

Time:03-09

The pandas dataframe 'pct' column was computed from the 'Paid' column using this:

df_payers['pct'] = df_payers['Paid'] / df_payers['Paid'].sum() * 100

Obtaining this dataframe:


  Payer    Paid                              pct
0  abc  50723.53   76.37589381959950316963498568
1  bcd   4724.00   7.113064142101073268626132139
2  cde   4198.00   6.321050649563993560900191092
3  def   3876.00   5.836205887972853511683930603
4  efg   1309.20   1.971300502717765690788596993
5  fgh   1292.28   1.945823566798131872053382312
6  ghi    290.00  0.4366614312466789263127811855

How do I round and format the 'pct' column to '{:,.2f%}'

CodePudding user response:

To round, but keep the column as float:

df_payers["pct"] = df_payers["pct"].round(2)

To convert to string with formatting:

df_payers["pct"] = df_payers['pct'].map("{:.2f}".format)
  • Related