Pandas – DataFrame to CSV file using tab separator

Let’s see how to convert a DataFrame to a CSV file using the tab separator. We will be using the to_csv() method to save a DataFrame as a csv file. To save the DataFrame with tab separators, we have to pass “\t” as the sep parameter in the to_csv() method.
Approach :
- Import the Pandas and Numpy modules.
- Create a DataFrame using the DataFrame() method.
- Save the DataFrame as a csv file using the to_csv() method with the parameter sep as “\t”.
- Load the newly created CSV file using the read_csv() method as a DataFrame.
- Display the new DataFrame.
Python3
# importing the modulesimport pandas as pdimport numpy as np# creating a DataFramestudents = {'Student': ['Amit', 'Cody', 'Darren', 'Drew'], 'RollNumber': [1, 5, 10, 15], 'Grade': ['A', 'C', 'F', 'B']}df = pd.DataFrame(students, columns =['Student', 'RollNumber', 'Grade'])# displaying the original DataFrameprint("Original DataFrame")print(df)# saving as a CSV filedf.to_csv('Students.csv', sep ='\t')# loading the CSV filenew_df = pd.read_csv('Students.csv')# displaying the new DataFrameprint('Data from Students.csv:')print(new_df) |
Output :
The contents of the Students.csv file are :




