如何将浮动百分比 pandas 数据框列格式化为 '{:,.2f%}'

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

pandas 数据框 'pct' 列是根据 'Paid' 列计算得出的:

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

获取此数据帧:


  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

如何将 'pct' 列舍入并格式化为 '{:,.2f%}'

四舍五入,但保持该列为浮点数:

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

要转换为带格式的字符串:

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