1

The problem is that i want to replace the values in the origin and dest columns in the df flights with the values from the dictionary airports but error is give cannot replace object with string. But the object dtype is equal to string right?

I looked for answers, people said it can be done manually but this is very hectic and other said i am using replace wrongly and i can use it like this but i don't understand how replace({'Keyword': {x:'other' for x in y}}, inplace=True)

airports=pd.read_csv('airports.csv')
airport_dict=pd.Series(airports.city.values,index=airports.iata).to_dict()
flights.Origin.replace(airport_dict.keys(), airport_dict.values(), inplace =True)
flights.Dest.replace(airport_dict.keys(), airport_dict.values(), inplace = True)

Values from csv file :

iata    airport
00M Thigpen 
00R Livingston Municipal
00V Meadow Lake

Values from Origin DF:

00M
00V 

and so on i want to replace them with the full names

rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • 1
    When you say "object dtype is equal to string", this is not the case. In numpy and pandas, it _is_ true than an array of strings will have the dtype 'object', but the error message in your title indicates that you are trying to replace a string with a numpy array, not with a string element _from_ a numpy array. Could you provide more detailed examples, as in [How to create good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)? This looks like a simple `df.merge()` from your description – G. Anderson Aug 05 '19 at 20:56
  • 2
    You can also use df.map() to look for the iata key from Origin and find the airport name. – Niels Henkens Aug 05 '19 at 21:01
  • Welcome to SO Yomna (:,if you post some sample data one of the experts here will be able to help you. – Umar.H Aug 05 '19 at 21:06

0 Answers0