Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing first row while reading from file - Python Pandas [duplicate]

Tags:

python

pandas

I have a file which has coordinates like

1  1
1  2
1  3
1  4
1  5

and so on

There are no zeros in them.I tried using comma and tab as a delimiter and still stuck in same problem. Now when I printed the output to screen I saw something very weird. It looks like it is missing the very first line.

The output after running pa.read_csv('co-or.txt',sep='\t') is as follows

   1  1
0   1  2
1   1  3
2   1  4
3   1  5

and so on.. I am not sure if I am missing any arguments in this.

Also when I tried to convert that to numpy array using np.array, It is again missing the first line and hence the first element [1 1]

like image 761
sridhar Avatar asked Oct 24 '25 13:10

sridhar


1 Answers

df = pd.read_csv('data.csv', header=None)

You need to specifcy header=None otherwise pandas takes the first row as the header.

If you want to give them a meaningful name you can use the names as such:

df = pd.read_csv('data.csv', header=None, names=['foo','bar'])

Spend some time with pandas Documentation as well to get yourself familiar with their API. This one is for read_csv

like image 104
Leb Avatar answered Oct 26 '25 01:10

Leb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!