I’m working with a pandas DataFrame that contains both string and float64 columns. When I export this data using to_csv(), large numbers get converted to scientific notation automatically. For instance, a value like 2847293847291.847382 becomes 2.847294e+12 in the output file.
I want to preserve the full numeric format without scientific notation. I attempted using the float_format parameter, but it fails because my DataFrame includes text columns alongside numeric ones.
Here’s my sample code:
import pandas as pd
import numpy as np
# Create sample data
data = pd.DataFrame({
'products': ['item1', 'item2', 'item3'],
'amounts': np.random.rand(3) * 50000000000000
})
# Export to CSV
data.to_csv('output.csv')
# Result shows scientific notation:
# ,products,amounts
# 0,item1,4.71892e+13
# 1,item2,1.83947e+13
# 2,item3,3.28475e+13
# Trying float_format causes error:
data.to_csv('output.csv', float_format='%.2f')
# ValueError: Unknown format code 'f' for object of type 'str'
I need the output to show full numbers like 47189200000000.50 instead of scientific notation. What’s the best approach to handle this mixed data type situation?
I’ve had good luck using options.display.float_format with to_string(), then writing the file manually. Ran into this same problem with financial data where precision was crucial.
with pd.option_context('display.float_format', '{:.2f}'.format):
csv_string = data.to_string(index=False)
with open('output.csv', 'w') as f:
f.write(csv_string.replace(' ', ','))
This keeps your original DataFrame intact - no permanent changes. The context manager won’t mess with your other operations either. You might need to tweak the string replacement for different separators, but you get full control over formatting and it handles mixed data types without issues.
This happens because pandas automatically switches to scientific notation for large floats when exporting to CSV. The easiest fix is converting your numeric columns to strings with proper formatting before calling to_csv.
Try this:
data_export = data.copy()
data_export['amounts'] = data_export['amounts'].map('{:.2f}'.format)
data_export.to_csv('output.csv')
This converts only the numeric values to formatted strings while leaving text columns alone. The map function applies string formatting to each value in the amounts column, so you get full control over decimal places without that mixed data type error.
For multiple numeric columns, identify them first:
numeric_cols = data.select_dtypes(include=[np.number]).columns
for col in numeric_cols:
data[col] = data[col].map('{:.2f}'.format)
data.to_csv('output.csv')
This works reliably across different pandas versions and completely avoids the float_format limitations.
set the quoting param - that’ll fix it. use quoting=csv.QUOTE_NONNUMERIC and convert your floats to strings first:
import csv
data['amounts'] = data['amounts'].astype(str)
data.to_csv('output.csv', quoting=csv.QUOTE_NONNUMERIC)
pandas treats everything as strings but keeps proper CSV formatting. saved my butt when product ids kept getting mangled.