使用pandas的to_csv导出DataFrame时出现数据丢失问题
Hey there! Let’s dig into why your pandas DataFrame might be losing rows or showing incomplete data when exporting to CSV, plus how to fix each issue:
1. You Accidentally Filtered or Modified the DataFrame Before Export
It’s easy to run a filter (like df = df[df['sales'] > 100]) or clean rows (like df.dropna()) and forget about it later. This would obviously cut down your row count before you even export.
- Quick fix: Right before calling
to_csv(), runprint(df.shape)to check if the row count matches what you expect. If it’s smaller, trace back your code to find where rows were removed. To avoid overwriting your original data, make a copy early on:df_backup = df.copy(), then compare shapes later if needed.
2. Delimiter Conflicts Are Messing Up Columns
If your data has commas (the default CSV delimiter) inside text fields—like an address column with "456 Oak Ave, Springfield"—exporting without adjusting settings will split those fields into extra columns. When you open the CSV, this makes rows look like they’re missing data (or have shifted values).
- Fixes:
- Use a different delimiter that doesn’t appear in your data, like a pipe or tab:
df.to_csv('output.csv', sep='|') - Wrap all text fields in quotes to preserve commas:
import csv df.to_csv('output.csv', quoting=csv.QUOTE_ALL)
- Use a different delimiter that doesn’t appear in your data, like a pipe or tab:
3. Memory Limits Are Truncating the Export
If your DataFrame is huge, your system might run out of memory mid-export, leaving you with only a partial CSV file.
- Fix: Export in smaller chunks with the
chunksizeparameter. This splits the data into manageable batches:df.to_csv('output.csv', chunksize=10000) # Adjust chunk size based on your system
4. Unintended Duplicate Row Removal
If you called df.drop_duplicates() somewhere in your code without specifying which columns to check, pandas will delete any rows that are identical across all columns. This can silently reduce your row count.
- Fix: First, check how many duplicates exist:
print(df.duplicated().sum()). If you want to keep duplicates, remove thedrop_duplicates()call. If you only want to remove duplicates for specific columns (like a user ID), specify them:df = df.drop_duplicates(subset=['user_id'])
5. Encoding Issues Are Corrupting Data
If your DataFrame has non-ASCII characters (like accents, emojis, or special symbols), using the default encoding might cause garbled text when you open the CSV in tools like Excel. This can make it look like data is missing or incomplete.
- Fix: Specify an encoding that supports your characters. For Excel-friendly UTF-8, use:
If you need compatibility with older systems, trydf.to_csv('output.csv', encoding='utf-8-sig')encoding='latin-1'(though it’s less ideal for Unicode).
6. File Path Permissions or Overwriting Issues
Sometimes, the file might not be saving fully if you don’t have write permissions for the target path, or if another program is already using the file. This can result in a partial CSV with fewer rows.
- Fix: Check that you have write access to the directory, and close any programs that might be opening the CSV file. You can also try saving to a different location to rule out path issues.
内容的提问来源于stack exchange,提问作者RajveerParikh

