Python Practice › Pandas Programs › Program 51
Write a tab-separated file
Write a pandas program to write a DataFrame to a CSV file with a tab separator.
Output
name age salary Alex 30 50000 John 27 65000
Approach
to_csv(sep="\t").
Solution
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "age": [30, 27], "salary": [50000, 65000]})df.to_csv("people.tsv", sep="\t", index=False)print(open("people.tsv").read())Output
name age salary Alex 30 50000 John 27 65000
Learn the concept
New to this topic? Read the lesson, then come back.