Reading feature names from a csv file using pandas

Viewed 921

I have a csv file looks like

        F1    F2    F3
 A1      2     4     2
 A2      4     1     2

When I read the file using pandas, I see that the first column is unnamed.

import pandas as pd
df = pd.read_csv("data.csv")
features = df.columns
print( features )

Index(['Unnamed: 0', 'F1, 'F2, 'F3'])

In fact I want to get only F1, F2 and F3. I can fix that with some array manipulation. But I want to know if pandas has some builtin capabilities to do that. Any thought?

UPDATE:

Using index_col = False or None doesn't work either.

enter image description here

1 Answers

That unnamed is only because of index column being read, you can use the index_col = [0] argument in the read statement to resolve.

This picks the first column as index instead of a feature itself.

import pandas as pd
df = pd.read_csv("data.csv", index_col=[0])
features = df.columns
print( features )

Index(['F1', 'F2', 'F3'])
Related