Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas read_csv, ignore first cell

I have .csv file like this:

Str;  Int; Flt
  A;  123; 0.1
  B;  456; 0.2
  C;  789; 0.3

I want to get DataFrame like this

   Int; Flt
A; 123; 0.1
B; 456; 0.2
C; 789; 0.3

I read csv like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = pd.read_csv('data.csv', index_col=0, sep=";")

And the problem is that I can’t use df.loc["A", "Int"] to get cell value.
If I drop Str; from csv everything works fine.

So the idea is to use first row as row names and first column as column names. I understand that first element can’t be used both as col name and row name, is there any way to drop such ambigous value?

>Solution :

Use: index_col='Str' instead of index_col=0

Edited script:

import pandas as pd 

df = pd.read_csv("test.csv", sep=";", index_col='Str')

print(df.loc["A", "Int"])

output: 123

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading