删除某些列中的小数位并保留其他列中的小数位

问题描述 投票:0回答:1

团队

我试图去掉几列中的小数,并将它们保留在其他列中。下面是我可悲的编码尝试。

我想去掉 RFW Tota、Hit Count、Cover Count、Tie Wins 和 Tie Away 中的小数。我想在点击率 %、覆盖率 %、平局率 % 和收入中保留小数,四舍五入到 2 位。

    'RFW Total': [RFQ_count, Vol_RAMT, DV01],
                   'Hit Count': [hit_count, hit_count_volume, hit_count_DV01],
                    'Hit Ratio %': [hit_ratio_count * 100, hit_ratio_volume * 100, hit_ratio_DV01 * 100],
                    'Cover Count': [cover_plus_count, cover_plus_volume, cover_plus_DV01],
                    'Cover Ratio %': [(cover_plus_count / RFQ_count) * 100, (cover_plus_volume / Vol_RAMT) * 100, (cover_plus_DV01 / DV01) * 100],
                    'Tie Wins': [tie_win_count , tie_win_volume, tie_win_DV01],
                    'Tie Away': [tie_away_count, tie_away_volume, tie_away_DV01],
                    'Tie Ratio %': [tie_win_count / tie_away_count, tie_win_volume / tie_away_volume, tie_win_DV01 / tie_away_DV01],
                    'Revenue': [(rev_count).round(decimals=3), (rev_count / hit_count_volume).round(decimals=2), (rev_count*1000000 / hit_count_DV01)]}
index_labels=["Count", "Volume (R-AMT) *mm", "DV01"] 
df1 = pd.DataFrame(DF_data, index=index_labels).round(decimals=2).style.format("{:,}")
# df1.style.format({'RFW Total': '{:,.1f}'.format, 'Hit Count': '{:,.1f}'.format, 'Hit Ratio %': '{:,.2f}'.format}
df1`

python pandas format decimal
1个回答
0
投票

astype
round
完成它。

columns_to_int = ['RFW Total', 'Hit Count', 'Cover Count', 'Tie Wins', 'Tie Away']
df = df.astype({col:'int' for col in columns_to_int})
columns_to_round = ['hit ratio %', 'cover ratio %', 'tie ratio %', 'Revenue']
df[columns_to_round] = df[columns_to_round].round(2)
© www.soinside.com 2019 - 2024. All rights reserved.