Issue
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.
Solution
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
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.