1

I have a dataset that contains flight information. One of the columns in the dataset is AirportFrom. This is a list of the 3 letter code attached to an airport.

I have a reference table that takes the 3 letter code and gives the state that the airport is in.

I want to create a new column that takes all of the data from the AiportFrom and basically assigns the related State to the specific airport in the new column.

I have tried a few things that none of them seem to work correctly. I am getting an error in the last time I was trying to do it.

airportState = {
  'ATL': 'Georgia',
  'AUS': 'Texas',
  'BNA': 'Tennessee',
  'BOS': 'Massachusetts',
  'BWI': 'Washington',
  'CLT': 'North Carolina',
  'DAL': 'Texas',
  'DCA': 'Virginia',
  'DEN': 'Colorado',
  'DFW': 'Texas',
  'DTW': 'Michigan',
  'EWR': 'New Jersey',
  'FLL': 'Florida',
  'HNL': 'Hawaii',
  'HOU': 'Texas',
  'IAD': 'Virginia',
  'IAH': 'Texas',
  'JFK': 'New York',
  'LAS': 'Nevada',
  'LAX': 'California',
  'LGA': 'New York',
  'MCO': 'Florida',
  'MDW': 'Illinois',
  'MIA': 'Florida',
  'MSP': 'Minnesota',
  'MSY': 'Louisiana',
  'OAK': 'California',
  'ORD': 'Illinois',
  'PDX': 'Oregon',
  'PHL': 'Pennsylvania',
  'PHX': 'Arizona',
  'RDU': 'North Carolina',
  'SAN': 'California',
  'SEA': 'Washington',
  'SFO': 'California',
  'SJC': 'California',
  'SLC': 'Utah',
  'SMF': 'California',
  'STL': 'Missouri',
  'TPA': 'Florida',

}

here is the code I am trying to run:

dataset['StateFrom'] = airportState[dataset['AirportFrom']]

I know what the issue is, but I am not sure how to fix it.

ojandali
  • 133
  • 7
  • 1
    You need to provide enough so that we can re-create what you're working on. Please share some of `dataset`. – BeRT2me Jul 17 '22 at 02:41
  • *I am getting an error* ... *I know what the issue is* ... what is the error and the issue? Please post data and full code for [mcve]. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Parfait Jul 17 '22 at 02:56

1 Answers1

1

Use map to substitute each value with the related State:

dataset['StateFrom'] = dataset['AirportFrom'].map(airportState)
print(dataset)

# Output
  AirportFrom StateFrom
0         PHX   Arizona
1         HNL    Hawaii
2         LGA  New York
Corralien
  • 109,409
  • 8
  • 28
  • 52