Pandas merge error TypeError: '>' not supported between instances of 'int' and 'str' Pandas merge error TypeError: '>' not supported between instances of 'int' and 'str' pandas pandas

Pandas merge error TypeError: '>' not supported between instances of 'int' and 'str'


This error indicates that indices in merged DF have different dtypes

Demo - how to convert string index level to int:

In [183]: dfOut[183]:              0         1         2         3bar 1 -0.205037  0.762509  0.816608 -1.057907    2  1.249104  0.338777 -0.982084  0.329330baz 1  0.845695 -0.996365  0.548100 -0.113733    2  1.247092 -2.674061 -0.071993 -0.734242foo 1 -1.233825 -0.195377 -0.240303  1.168055    2 -0.108942 -0.615612 -1.299512  0.908641qux 1  0.844421  0.251425 -0.506877  1.307800    2  0.038580  0.045072 -0.262974  0.629804In [184]: df.indexOut[184]:MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['1', '2']],           labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]])In [185]: df.index.get_level_values(1)Out[185]: Index(['1', '2', '1', '2', '1', '2', '1', '2'], dtype='object')In [187]: df.index = df.index.set_levels(df.index.get_level_values(1) \                       .map(lambda x: pd.to_numeric(x, errors='coerce')), level=1)

Result:

In [189]: df.index.get_level_values(1)Out[189]: Int64Index([1, 2, 1, 2, 1, 2, 1, 2], dtype='int64')

UPDATE: try this:

In [247]: d1 = pd.read_csv('https://docs.google.com/uc?id=1jUsbr5pw6sUMvewI4fmbpssroG4RZ7LE&export=download', index_col=[0,1])In [248]: d2 = pd.read_csv('https://docs.google.com/uc?id=1Ufx6pvnSC6zQdTAj05ObmV027fA4-Mr3&export=download', index_col=[0,1])In [249]: d2 = d2[pd.to_numeric(d2.index.get_level_values(1), errors='coerce').notna()]In [250]: d2.index = d2.index.set_levels(d2.index.get_level_values(1).map(lambda x: pd.to_numeric(x, errors='coerce')), level=1)In [251]: d1.reset_index().merge(d2.reset_index(), on=['Country','Year'], how='outer').set_index(['Country','Year'])Out[251]:                            ind500  ind356  ind475  ind476        ind456Country               YearAfghanistan           1800   603.0     NaN     NaN     NaN           NaN                      1801   603.0     NaN     NaN     NaN           NaN                      1802   603.0     NaN     NaN     NaN           NaN                      1803   603.0     NaN     NaN     NaN           NaN                      1804   603.0     NaN     NaN     NaN           NaN                      1805   603.0     NaN     NaN     NaN           NaN                      1806   603.0     NaN     NaN     NaN           NaN                      1807   603.0     NaN     NaN     NaN           NaN                      1808   603.0     NaN     NaN     NaN           NaN                      1809   603.0     NaN     NaN     NaN           NaN...                            ...     ...     ...     ...           ...Bahamas, The          1967     NaN     NaN     NaN     NaN  18381.131314Gambia, The           1967     NaN     NaN     NaN     NaN    937.355288Korea, Dem. Rep.      1967     NaN     NaN     NaN     NaN   1428.689253Lao PDR               1967     NaN     NaN     NaN     NaN   1412.359955Netherlands Antilles  1967     NaN     NaN     NaN     NaN  14076.731352Russian Federation    1967     NaN     NaN     NaN     NaN  11794.726437Serbia and Montenegro 1967     NaN     NaN     NaN     NaN   2987.080489Syrian Arab Republic  1967     NaN     NaN     NaN     NaN   2015.913906Yemen, Rep.           1967     NaN     NaN     NaN     NaN   1075.693355Bahamas, The          1968     NaN     NaN     NaN     NaN  18712.082830[46607 rows x 5 columns]


For anyone who stumbles across this in 2021:

The problem here is that the pandas multi-index is not unique in the dataset.

You can solve this either by:

  • Selecting a unique multi-index
  • Or, resetting the index and doing a merge on the columns

eg. pd.merge(d1.reset_index(), d2.reset_index(), on=['Country','Year'], how='outer')