How to write Pandas DataFrame as TSV using Python?


Pandas dataframe can be written as a tab separated Value (TSV) using the to_csv() method of Pandas library. Pandas is a powerful data manipulation and analysis library in Python. It provides various functionalities to work with structured data, including reading and writing data in different formats. One common format for storing tabular data is TSV (Tab-Separated Values), where columns are separated by tabs. In this article,we will understand with examples how to write a Pandas Dataframe to a TSV file using Python.

Algorithm

To write a Pandas DataFrame as a TSV file, we can follow these steps:

  • Import the necessary libraries: We need to import the pandas library to work with DataFrames.

  • Create a DataFrame: We need to create or obtain the DataFrame that we want to write as a TSV file. This can be done by reading data from a file, a database, or creating a DataFrame from scratch.

  • Specify the output file path: Decide on the file path and name where you want to save the TSV file.

  • Write the DataFrame to a TSV file: Use the to_csv() function from Pandas to write the DataFrame to the specified file path. Set the sep parameter to '\t' to indicate that the columns should be separated by tabs.

  • Verify the output: Check if the TSV file has been successfully created and contains the desired data.

Writing a Dataframe as a TSV file

A DataFrame is a two-dimensional tabular data structure provided by the Pandas library in Python. It is designed to store and manipulate structured data, similar to a table or a spreadsheet.

TSV is a file format used to store and exchange tabular data, where columns are separated by tabs ("\t"). TSV files are similar to CSV (Comma-Separated Values) files, but instead of using commas as separators, they use tabs.

We can write a Dataframe as a TSV file in Python using the to_csv() method provided by pandas library. In the below example we will write the employee dataframe containing information of the employees to a TSV file.

Syntax

df.to_csv(output_file, sep='\t', index=False, header=True)

Here, the parameters used are :

  • df: The DataFrame that you want to write as a TSV file.

  • output_file: The path and name of the output TSV file.

  • sep: The separator used to separate columns in the TSV file. Set it to '\t' for tab separation.

  • index: Whether to include the index column in the output file. Set it to False to exclude the index column. It is optional

  • header: Whether to include the column names as the first row in the output file. Set it to True to include the column names. It is optional

Example

In the below example, we import the pandas library and create a DataFrame named df with columns 'Name', 'Age', and 'Salary'. We specify the output file path as 'employees.tsv'. The to_csv() function is used to write the DataFrame to the specified file path. We set sep='\t' to indicate that the columns should be separated by tabs. The index=False parameter is used to exclude the index column from the output file. Finally, we print a success message indicating that the DataFrame has been written as a TSV file.

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['John', 'Alice', 'Bob'],
    'Age': [28, 32, 45],
    'Salary': [50000, 60000, 75000]
}
df = pd.DataFrame(data)

# Specify the output file path
output_file = 'employees.tsv'

# Write the DataFrame to a TSV file
df.to_csv(output_file, sep='\t', index=False)

print(f"DataFrame successfully written as {output_file} \n {df}")

Output

DataFrame successfully written as employees.tsv 
     Name  Age  Salary
0   John   28   50000
1  Alice   32   60000
2    Bob   45   75000

Writing a Dataframe with custom column names as a TSV file

Sometimes we may want to write a DataFrame with custom column names as a TSV file. In this case we can provide custom names to the column before writing the dataframe to a csv file.

Example

In the below example, we create a DataFrame named df with columns 'Product', 'Units Sold', and 'Revenue'. We specify the output file path as 'sales.tsv'. We also specify custom column names using the header parameter, which is set to column_names. The rest of the steps are similar to the process of writing normal dataframe to TSV value.

import pandas as pd

# Create a DataFrame with custom column names
data = {
    'Product': ['A', 'B', 'C'],
    'Units Sold': [100, 200, 150],
    'Revenue': [5000, 8000, 6000]
}
df = pd.DataFrame(data)

# Specify the output file path
output_file = 'sales.tsv'

# Specify custom column names
column_names = ['Product Name', 'Total Units', 'Total Revenue']

# Write the DataFrame to a TSV file with custom column names
df.to_csv(output_file, sep='\t', index=False, header=column_names)

print(f"DataFrame with custom column names successfully written as {output_file}")

Output

DataFrame with custom column names successfully written as sales.tsv

Conclusion

In this article we have discussed how we can write pandas dataframe as a TSV using the to_csv() method of pandas library. By specifying the output file path, separator, and other optional parameters, we can easily generate TSV files containing the data from our DataFrames. We can also add custom names to the column when writing the dataframto a TSV file.

Updated on: 16-Oct-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements