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

Viewed 752

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
2 Answers

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

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
Related