When exporting data from a Pandas DataFrame to a CSV file, the default delimiter is a comma (,). However, many applications and data processing tools expect tab-separated values (TSV) instead. Tab-separated files are particularly useful when your data contains commas within field values, as tabs are far less common in natural text.
In this guide, you will learn how to save a DataFrame to a tab-separated CSV file using Pandas' to_csv() method, read it back correctly, and handle common pitfalls.
Saving a DataFrame With Tab Separator
Use the sep parameter of to_csv() to specify the tab character (\t) as the delimiter:
import pandas as pd
students = pd.DataFrame({
'Student': ['Amit', 'Cody', 'Darren', 'Drew'],
'RollNumber': [1, 5, 10, 15],
'Grade': ['A', 'C', 'F', 'B']
})
print("Original DataFrame:")
print(students)
students.to_csv('Students.tsv', sep='\t', index=False)
print("\nFile saved as 'Students.tsv'")Output:
Original DataFrame:
Student RollNumber Grade
0 Amit 1 A
1 Cody 5 C
2 Darren 10 F
3 Drew 15 B
File saved as 'Students.tsvThe resulting Students.tsv file contains:
Student RollNumber Grade
Amit 1 A
Cody 5 C
Darren 10 F
Drew 15 BEach value is separated by a tab character instead of a comma.
Reading the Tab-Separated File Back
When loading a tab-separated file, specify sep='\t' in read_csv() to parse it correctly:
import pandas as pd
df = pd.read_csv('Students.tsv', sep='\t')
print("Data loaded from Students.tsv:")
print(df)Output:
Data loaded from Students.tsv:
Student RollNumber Grade
0 Amit 1 A
1 Cody 5 C
2 Darren 10 F
3 Drew 15 BControlling the Index Column
By default, to_csv() includes the DataFrame index as the first column. Use index=False to exclude it:
students.to_csv('with_index.tsv', sep='\t')File contents:
Student RollNumber Grade
0 Amit 1 A
1 Cody 5 Cstudents.to_csv('without_index.tsv', sep='\t', index=False)File contents:
Student RollNumber Grade
Amit 1 A
Cody 5 CAdditional Useful Parameters
The to_csv() method supports many parameters for fine-tuning the output:
import pandas as pd
students = pd.DataFrame({
'Student': ['Amit', 'Cody', None, 'Drew'],
'RollNumber': [1, 5, 10, 15],
'Grade': ['A', 'C', 'F', 'B']
})
students.to_csv(
'Students.tsv',
sep='\t', # Tab separator
index=False, # Exclude index
header=True, # Include column names (default)
na_rep='N/A', # Replace NaN/None with 'N/A'
encoding='utf-8', # Specify encoding
columns=['Student', 'Grade'] # Export only specific columns
)File contents:
Student Grade
Amit A
Cody C
N/A F
Drew BParameter Quick Reference
| Parameter | Description | Default |
|---|---|---|
sep |
Delimiter between values | , |
index |
Include row index | True |
header |
Include column names | True |
na_rep |
String to represent missing values | '' (empty) |
encoding |
File encoding | 'utf-8' |
columns |
Subset of columns to export | All columns |
float_format |
Format string for floats (e.g., '%.2f') |
None |
quoting |
Control quoting behavior | csv.QUOTE_MINIMAL |
Handling Data With Tabs in Values
If your data itself contains tab characters, they will break the TSV format. Use the quoting parameter to wrap such values in quotes:
import pandas as pd
import csv
df = pd.DataFrame({
'Name': ['Alice', 'Bob\tSmith'], # Tab in value!
'Score': [95, 88]
})
df.to_csv('safe.tsv', sep='\t', index=False, quoting=csv.QUOTE_ALL)File contents:
"Name" "Score"
"Alice" "95"
"Bob Smith" "88"Complete Example: Round-Trip Export and Import
import pandas as pd
import numpy as np
data = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [92.5, 85.3, 78.9, 95.1],
'Passed': [True, True, False, True]
})
print("Original DataFrame:")
print(data)
print(f"Shape: {data.shape}")
data.to_csv('results.tsv', sep='\t', index=False)
loaded = pd.read_csv('results.tsv', sep='\t')
print("\nLoaded DataFrame:")
print(loaded)
print(f"Shape: {loaded.shape}")
print(f"\nDataFrames are equal: {data.equals(loaded)}")Output:
Original DataFrame:
Name Age Score Passed
0 Alice 25 92.5 True
1 Bob 30 85.3 True
2 Charlie 35 78.9 False
3 Diana 28 95.1 True
Shape: (4, 4)
Loaded DataFrame:
Name Age Score Passed
0 Alice 25 92.5 True
1 Bob 30 85.3 True
2 Charlie 35 78.9 False
3 Diana 28 95.1 True
Shape: (4, 4)
DataFrames are equal: TrueConclusion
Saving a Pandas DataFrame to a tab-separated CSV file is as simple as passing sep='\t' to the to_csv() method. Use index=False to avoid an extra index column, na_rep to control how missing values appear, and quoting=csv.QUOTE_ALL if your data might contain tab characters.
When reading the file back, always specify sep='\t' in read_csv() to ensure proper parsing.
Tab-separated files are an excellent choice when your data contains commas, as they avoid the ambiguity that comma-separated formats can introduce.