Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shift the column headers in pandas

Tags:

pandas

I have .txt files I'm reading in with pandas and the header line starts with '~A'. I need to ignore the '~A' and have the next header correspond to the data in the first column. Thanks!

like image 609
user5552109 Avatar asked Nov 01 '25 15:11

user5552109


1 Answers

You can do this:

import pandas as pd
data = pd.read_csv("./test.txt", names=[ 'A', 'B' ], skiprows=1)    
print(data)

and the output for input:

~A, A, B
1, 2
3, 4

is:

c:\Temp\python>python test.py

   A  B
0  1  2
1  3  4

You have to name the columns yourself but given that your file seems to be malformed I guess it is not that bad.

If your header lines are not the same in all files, then you can just read them in Python:

import pandas as pd;

# read first line
with open("./test.txt") as myfile:
    headRow = next(myfile)

# read column names    
columns = [x.strip() for x in headRow.split(',')]

# process by pandas
data = pd.read_csv("./test.txt", names=columns[1:], skiprows=1)

print(data);
like image 162
Martin Vseticka Avatar answered Nov 03 '25 20:11

Martin Vseticka