How did you match coordinates for each airport?

I tried to create the necessary DataFrame by myself as you did in file geo_route.csv.
I tried to match longitude and altitude for each route (both for source and destination).

My Code:

airports = pd.read_csv('airports.csv',  index_col = 4) 
airports.index.name = None
# airports with index = code of each airport

routes_sourse_index = pd.read_csv('routes.csv' , index_col = 2 )
routes_sourse_index.index.name = None
# df routes_sourse_index with index = code of source

routes_sourse_index['start_latitude'] = airports['latitude']
# trying to add new column to df routes_sourse_index named 'start_latitude'

I expected that the code will match by code of the airport (index) the latitude value of an airport to a source .

ValueErrorTraceback (most recent call last)
<ipython-input-1-bd5ab21e6058> in <module>()
      4 routes_sourse_index = pd.read_csv('routes.csv' , index_col = 2 )
      5 routes_sourse_index.index.name = None
----> 6 routes_sourse_index['start_latitude'] = airports['latitude']

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
   2517         else:
   2518             # set column
-> 2519             self._set_item(key, value)
   2520 
   2521     def _setitem_slice(self, key, value):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in _set_item(self, key, value)
   2583 
   2584         self._ensure_valid_index(value)
-> 2585         value = self._sanitize_column(key, value)
   2586         NDFrame._set_item(self, key, value)
   2587 

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in _sanitize_column(self, key, value, broadcast)
   2737 
   2738         if isinstance(value, Series):
-> 2739             value = reindexer(value)
   2740 
   2741         elif isinstance(value, DataFrame):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in reindexer(value)
   2729                     # duplicate axis
   2730                     if not value.index.is_unique:
-> 2731                         raise e
   2732 
   2733                     # other

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in reindexer(value)
   2724                 # GH 4107
   2725                 try:
-> 2726                     value = value.reindex(self.index)._values
   2727                 except Exception as e:
   2728 

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/series.py in reindex(self, index, **kwargs)
   2679     @Appender(generic._shared_docs['reindex'] % _shared_doc_kwargs)
   2680     def reindex(self, index=None, **kwargs):
-> 2681         return super(Series, self).reindex(index=index, **kwargs)
   2682 
   2683     @Appender(generic._shared_docs['fillna'] % _shared_doc_kwargs)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in reindex(self, *args, **kwargs)
   3021         # perform the reindex on the axes
   3022         return self._reindex_axes(axes, level, limit, tolerance, method,
-> 3023                                   fill_value, copy).__finalize__(self)
   3024 
   3025     def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value,

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
   3039             obj = obj._reindex_with_indexers({axis: [new_index, indexer]},
   3040                                              fill_value=fill_value,
-> 3041                                              copy=copy, allow_dups=False)
   3042 
   3043         return obj

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in _reindex_with_indexers(self, reindexers, fill_value, copy, allow_dups)
   3143                                                 fill_value=fill_value,
   3144                                                 allow_dups=allow_dups,
-> 3145                                                 copy=copy)
   3146 
   3147         if copy and new_data is self._data:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/internals.py in reindex_indexer(self, new_axis, indexer, axis, fill_value, allow_dups, copy)
   4137         # some axes don't allow reindexing with dups
   4138         if not allow_dups:
-> 4139             self.axes[axis]._can_reindex(indexer)
   4140 
   4141         if axis >= self.ndim:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/base.py in _can_reindex(self, indexer)
   2942         # trying to reindex on an axis with duplicates
   2943         if not self.is_unique and len(indexer):
-> 2944             raise ValueError("cannot reindex from a duplicate axis")
   2945 
   2946     def reindex(self, target, method=None, level=None, limit=None,

ValueError: cannot reindex from a duplicate axis

But if i try to use a short slice of the Series , it works:

routes_sourse_index[‘start_latitude’] = airports[‘latitude’][0:100]
A new column has been added.

The error usually generates when you concatenate, reindexing or resampling a DataFrame which the index has duplicate values . When you get this “cannot reindex from a duplicate axis” error, first you have to just check if there is any duplication in your DataFrame column names using the code:

df[df.index.duplicated()]

If DataFrame has duplicate index values , then remove the duplicated index:

df= df.loc[~df.index.duplicated(), :]