ech0
ech0

Reputation: 37

How to read a CSV Column with space in name using panda library in python

So lets say I have a column in my csv file named "how are you" how can I read the contents of this column? Right now I am changing the column name in csv file and running this, it works but how can I do it without changing anything in the csv file?

df1 = pd.read_csv('file.csv')
x = df1.howareyou

Upvotes: 2

Views: 1969

Answers (2)

vxxxi
vxxxi

Reputation: 345

samutamm's answer definitely works.

However, if you like to access the columns using dot notation, you can replace the spaces with '_'. e.g:

df.columns = df.columns.str.replace(' ', '_')
df.how_are_you

Upvotes: 0

samu
samu

Reputation: 2036

You can access a column like this : x = df1["how are you"]

Upvotes: 1

Related Questions