Reputation: 2605
I have a pandas data frame were currently all the columns are floats, which I am exporting to a csv file using DF.to_csv.
I would like for one of the columns to be exported as an int instead of as a float. A second columns has numbers with a lot of decimals and is being exported in scientific notation format. I want to to be exported as a regular decimal number up to a certain degree of precision, not in scientific notation.
Say my DF is called DataOut and has columns 'A', 'B' and 'C'
Is there anything I can add to
DataOut.to_csv(filename, mode = 'w', header = False , index=False)
So that the values in A are exported as int, and the values in B are exported as decimals with a maximum precision of 20 digits ?
Upvotes: 4
Views: 14666
Reputation: 4090
Make a copy of your dataframe, round the respective columns to ints, and export the CSV:
import pandas as pd
import random
#generate a dataframe with two columns, both floating point
df = pd.DataFrame({'A':[random.random()*10 for i in range(10)],
'B':[random.random()*20 for i in range(10)]})
df2 = df.copy() #make a copy to preserve your original
df2.loc[:, 'A'] = df2['A'].apply(int) #convert A to an int
df2.loc[:, 'B'] = df2['B'].round(20) #round B to 20 points of precision
df2.to_csv('test.csv', header = None, index = False)
Upvotes: 3
Reputation: 555
for the float
Which works similarly for to_csv:
df.to_csv('df.csv', float_format='{:f}'.format, encoding='utf-8')
Source https://stackoverflow.com/a/23006399/4941927 Probably with float_format also can convert to int, but i dont know.
for the int convertion I think that could use a round() function and a generator before parser to plain file, but i'm sure because i never use panda
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
I would see your complete code @AlexKinman
Upvotes: 1