Q.Besides comma, what are the other allowed characters that can be used as a separator while creating a CSV file frmo a DataFrame?
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Besides comma, any single character can be used as a separator in to_csv() via the sep parameter — most commonly tab (\t), pipe (|), semicolon (;), colon (:), or space.
When you write a DataFrame to a CSV file using df.to_csv(), the method does not restrict you to commas. The name "CSV" stands for Comma-Separated Values, but the underlying format is simply a delimited text file, and Pandas allows you to choose any delimiter that suits your data or the system you're exporting to.
The sep parameter (short for separator) controls this. By default it is ',', but you can pass any single character. The choice depends on what your data contains and what the receiving application expects.
Commonly Used Separators
| Separator | Character | sep value | Use case |
|---|---|---|---|
| Tab | Horizontal tab | '\t' | TSV files (Tab-Separated Values); cleaner when data contains commas |
| Pipe | Vertical bar | `' | '` |
| Semicolon | ; | ';' | European CSV standard (where comma is decimal separator) |
| Colon | : | ':' | Log files, configuration exports |
| Space | Single space | ' ' | Simple text tables (though alignment suffers) |
| Custom | Any character | e.g. '~', '#' | Domain-specific formats |
Example
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob'],
'Score': [85, 92]
})
# Tab-separated
df.to_csv('data.tsv', sep='\t', index=False)
# Pipe-separated
df.to_csv('data.txt', sep='|', index=False)
# Semicolon-separated
df.to_csv('data_eu.csv', sep=';', index=False)
Output of data.tsv (tab-separated):
Name Score
Alice 85
Bob 92
Output of data.txt (pipe-separated):
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.