danbuza
danbuza

Reputation: 144

Pandas columns are showing in '.head()', but not in '.columns'

I imported the HousePrices dataset from Kaggle, displayed the rows at the beginning, but when calling df.columns, I only get the following output:

enter image description here

Calling df.colums:

houseprices.columns

output:

Index(['sep=', 'Unnamed: 1'], dtype='object')

I don't have any idea of what is happening hear, so I would be grateful for an explanation and how to fix it.

Upvotes: 1

Views: 1046

Answers (1)

LoukasPap
LoukasPap

Reputation: 1381

You need to specify in pd.read_csv if the dataframe contains column names. Because you did not do that, the columns are the first row of your dataframe. That's why when you use .head(), you get the 5 first rows, from which the first is the columns' names.

Try this:

houseprices = pd.read_csv('.\\trains.csv', sep=',', header=0)

Read the full documentation here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

Upvotes: 1

Related Questions