0

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%}'

MeirG
  • 333
  • 2
  • 14
  • Does this answer your question? [How to display pandas DataFrame of floats using a format string for columns?](https://stackoverflow.com/questions/20937538/how-to-display-pandas-dataframe-of-floats-using-a-format-string-for-columns) – cfort Mar 08 '22 at 18:02
  • Dear @cfort, Well, yes, but only the one that actually modifies the percentage columns! – MeirG Mar 08 '22 at 20:08

1 Answers1

2

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)
not_speshal
  • 22,093
  • 2
  • 15
  • 30