Python – pandas to_csv parameter float_format to percentage

pandas to_csv parameter float_format to percentage… here is a solution to the problem.

pandas to_csv parameter float_format to percentage

I’m trying to output a pandas data frame, but I want it to output float as a percentage. I’ve searched this a lot and I can get them to appear as floats in the output, just like I want to use

pd.options.display.float_format = '{:,.2%}'.format

But what I want is to do exactly the same thing, except when I export to .csv using the .to_csv method.

Sample code:

df2 = pd. DataFrame(data = {'Index': ['One', 'Two'], 'col1': [.1, .2], 'col2': [.3, .4], 'col3': [1, 7]})
df2.set_index('Index')

df:

    col1    col2    col3
Index           
One 0.1     0.3     1
Two 0.2     0.4     7

col1 and col2 are float64, col3 is an integer

I’d like to use the following (which could be something similar, of course):

dfs.to_csv("filelocation and name.csv", float_format = '{:.2%}'.format)

The output is .csv as follows:

    col1    col2    col3
Index           
One 10%     30%     1
Two 20%     40%     7

Any help would be appreciated. I encountered errors ranging from “TypeError: unsupported format string passed to numpy.ndarray._ _ format _ _” to “KeyError: ‘%'” and something in between.

Solution

You can access float_format to format the output in the CSV file, but this is just a string, which means you can format columns, but not manipulate them. For example

with open('foo.csv', 'w') as f:
    df2.to_csv(f, index = False, header = True, float_format = '%.2f%%')

This output is created

Index,col1,col2,col3
One,0.10%,0.30%,1
Two,0.20%,0.40%,7

If you want to change the value, I suggest you update the dataframe before outputting it

df2 = pandas. DataFrame(data = {'Index': ['One', 'Two'], 'col1': [.1, .2], 'col2': [.3, .4], 'col3': [1, 7]})
df2.set_index('Index')

df2['col1'] *= 100
df2['col2'] *= 100

with open('foo.csv', 'w') as f:
    df2.to_csv(f, index = False, header = True, float_format = '%.2f%%')

Related Problems and Solutions