I’m working with a Pandas DataFrame that has both float64 and string columns. When I try to save it as a CSV file using to_csv(), the large numbers are converted to scientific notation. For instance, 1344154454156.992676 becomes 1.344154e+12 in the output file.
I want to keep the original number format without scientific notation. I tried using the float_format parameter, but it didn’t work because of the string columns in my DataFrame.
This creates a CSV file with scientific notation for the ‘big_num’ column. How can I save the CSV file with the full number representation instead? I’m looking for a solution that works with mixed data types. Any help would be appreciated!
I’ve encountered this issue before when working with large datasets. One effective solution I found is to convert the numeric columns to strings before exporting. You can do this by applying a lambda function to the ‘big_num’ column:
Then, when you use to_csv(), the numbers will be written as strings, preserving their full representation:
df.to_csv(‘output.csv’, index=False)
This method works well with mixed data types and doesn’t affect string columns. It also gives you control over the number of decimal places. Just adjust the .2f in the lambda function as needed for your specific requirements.
Remember to convert the column back to numeric if you need to perform calculations later.
I’ve dealt with this exact issue before, and I found a neat workaround that might help you out. Instead of using to_csv() directly, you can first convert your DataFrame to a string representation using to_string(), and then write that to a file. Here’s how you can do it:
df_string = df.to_string(index=False, header=True, float_format=lambda x: f'{x:.2f}')
with open('output.csv', 'w') as f:
f.write(df_string.replace(' ', ','))
This approach gives you full control over the number formatting without affecting your string columns. The float_format parameter in to_string() lets you specify exactly how you want your numbers to appear. You can adjust the ‘.2f’ to show more or fewer decimal places as needed.
One thing to watch out for: this method will use spaces as separators by default, which is why we use replace() to swap them for commas. Just make sure your string data doesn’t contain any spaces that might get affected by this replacement.