karan naithani
karan naithani

Reputation: 1

Coverting Tab Delimited file to CSV

I am trying to load a file in Python (3.7) and use it to generate a few plots. the file is tab delimited (.dat extension). I somehow managed to convert this file to a .csv file but then the header is shifted by 1 column. Please help !

Code:

import pandas as pd

df = pd.read_csv('p2s-a 062019.dat', delimiter = '\t')

df.to_csv('p2s-a 062019.csv', encoding = 'utf-8', index = False)

newdf = pd.read_csv('p2s-a 062019.csv')

newdf

Image attached

Table with header offset

Upvotes: 0

Views: 100

Answers (1)

Roy2012
Roy2012

Reputation: 12493

Try doing this:

df.columns = list(df.columns[1:]) + ["to_delete"]

df.drop("to_delete", axis=1)

It'll shift all the columns by one, and delete the extra one that you don't need at the end.

Upvotes: 1

Related Questions