0

So I read several questions about this problem and most times the recommended approach is to use pd.options.display.float_format. But the thing is, I need to consecutively print dataframes as floats, and then present the percent change between two tables (as percent), so adding a pd.options.display.float_format line before each print seems not very straightforward. In the first phase I was just printing series, so I could use `pd.Series.map(lambda x: f'{x:.2%}')}" but this won't work with DataFrames.

Is there a better approach to this? Thanks.

Just to be clear, I have two kinds of dataframes:

df
col1    col2
0.02    0.1
0.06    0.8

df2
col3    col4
0.123    0.23
0.36    0.21

I create several of those in my code and the first one is always scores that should be presented as two-decimal floats (easy to do with round), and the second ones are always percentages and should be presented as such:

df2
col3        col4
12.30%      23.00%
36.00%      21.00%

Is there any way to print a dataframe of floats as percentages without having to previously specify a pd.options.display.float_format , similar to what I did with series using map?

Juan C
  • 5,846
  • 2
  • 17
  • 51
  • Take a look at the answer to https://stackoverflow.com/questions/20937538/how-to-display-pandas-dataframe-of-floats-using-a-format-string-for-columns, specifically https://stackoverflow.com/a/23922119/. – AMC Mar 25 '20 at 19:09
  • Didn't catch that answer. Thank you very much, that solves my problem ! – Juan C Mar 25 '20 at 19:11

0 Answers0