How to prevent scientific notation when exporting pandas DataFrame with mixed data types to CSV

I’m working with a pandas DataFrame that contains both string and float64 columns. When I export this data to CSV using to_csv(), large numbers get converted to scientific notation automatically. For instance, a value like 2847592847593.847392 becomes 2.847593e+12 in the output file.

I need to preserve the full numeric format without scientific notation. I attempted using the float_format parameter, but it fails because my DataFrame includes string columns as well.

Here’s my sample code:

import pandas as pd
import numpy as np

# Create sample data
data = pd.DataFrame({
    'categories': ['x', 'y', 'z'],
    'amounts': np.random.rand(3) * 500000000000000
})

# Standard export shows scientific notation
data.to_csv('output.csv')

# Attempting float formatting causes error
data.to_csv('output.csv', float_format='%.6f')
# Error: ValueError - format issue with string columns

What’s the best approach to export CSV files with full number precision while handling mixed column types?

try converting float columns to strings b4 exporting: data['amounts'] = data['amounts'].astype(str) then use to_csv(). it’s a bit hacky but does the job with mixed types without that annoying sci-notation.

You can use pd.options.display.float_format globally, but I’d go with the quoting parameter instead. Just do data.to_csv('output.csv', quoting=csv.QUOTE_NONNUMERIC) after importing csv. It’ll quote all non-numeric data while keeping full precision on numbers. Had the same problem with financial datasets - account numbers and transaction amounts. The quoting method kept everything intact without messing with the original DataFrame or converting columns one by one.

The float_format parameter doesn’t work like you’d think. Wrap it in a lambda function to handle mixed datatypes - try data.to_csv('output.csv', float_format=lambda x: '%.6f' % x) instead of direct string formatting. It’ll only process numeric values and leave string columns alone. Had this exact problem last month with financial data exports and this fixed it without messing with the original DataFrame or converting columns to strings first.