RedRum
RedRum

Reputation: 982

Why did I my column names change to numbers when I did not assign them?

My code looks like this

import pandas as pd

df = pd.read_csv('FlightDistanceTest.csv')
df.rename(columns={'Normalised City Pair': 'destination'}, inplace=True)
split_city = df['destination'].str.split(' - ', expand=True)
df = pd.concat([df, split_city], axis=1)
df.columns = list(range(len(df.columns)))

The original df looked like this but with the code above, I was able to split the airport column by the - so that the cities were split into 2 new columns and then I concatenated them to the original df. My issue is.. ALL the columns names have been changed to 1, 2, 3, 4, 5.. which I did not assign.Even when I save it to a csv, the column names are 1, 2, 3, 4, 5 in excel... what did I do wrong? Thanks

old_index   destination                                 arr dep
0   319         London, United Kingdom - Paris, France  CDG LHR
1   320         London, United Kingdom - Paris, France  CDG LHR
2   321         London, United Kingdom - Paris, France  CDG LHR
3   322         London, United Kingdom - Paris, France  CDG LHR
4   323         London, United Kingdom - Paris, France  CDG LHR
5   324         Dusseldorf, Germany - Paris, France     CDG DUS
6   325         Amsterdam, Netherlands - Paris, France  CDG AMS
7   326         Amsterdam, Netherlands - Paris, France  CDG AMS
8   327         Amsterdam, Netherlands - Paris, France  CDG AMS
9   328         Amsterdam, Netherlands - Paris, France  CDG AMS
10  329         Amsterdam, Netherlands - Paris, France  CDG AMS

Upvotes: 0

Views: 197

Answers (2)

Vinay
Vinay

Reputation: 136

Do not use last line code -

df.columns = list(range(len(df.columns)))

This will rename your DataFrame columns to 0,1,2,3,.....

Upvotes: 0

GabrielP
GabrielP

Reputation: 782

just delete the last row.

df.columns = list(range(len(df.columns))) changes your column names to numbers.

Upvotes: 1

Related Questions