0

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 
2
  • I'm not entirely sure, I copy and pasted the code which worked great (except for the column name change) but from what I understand, the Len function is measuring the length of the columns (which is 5), then the range is taking the range of columns (0:5), then list converts it to a list? How would I change my code so that I retain my original column names? Commented Aug 7, 2021 at 14:20
  • Yep, you have that exactly right. If you're happy with the column names prior to that step, then just deleting that line is enough to give you want you want. Otherwise, you can change it to rename them to something else. Commented Aug 7, 2021 at 14:22

2 Answers 2

1

just delete the last row.

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

Sign up to request clarification or add additional context in comments.

Comments

0

Do not use last line code -

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.