Saturday, November 22, 2025
HomeLanguagesHow to write Pandas DataFrame as TSV using Python?

How to write Pandas DataFrame as TSV using Python?

In this article, we will discuss how to write pandas dataframe as TSV using Python.

Let’s start by creating a data frame. It can be done by importing an existing file, but for simplicity, we will create our own.

Python3




# importing the module
import pandas as pd
  
# creating some sample data
sample = {'name': ['a', 'b', 'c', 'd'],
         'age': [24, 65, 39, 18]}
  
# creating the DataFrame
df = pd.DataFrame(sample)
  
# displaying the DataFrame
print(df)


Output:

  name  age
0    a   24
1    b   65
2    c   39
3    d   18

Now, let’s export this as a TSV file. We can use to_csv method from pandas for this.

Syntax: df.to_csv(” file.tsv”, sep = “”)

Example:

Python3




# saving as tsv file
df.to_csv('example.tsv', sep="\t")


Output:

Here, sep defines what is the separator which separates the data entries in the file. In this case, we define it as a tabspace (‘\t’).  It will create a .csv file by default if the separator is not defined.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS