title: Pandas merge error TypeError: '>' not supported between instances of 'int' and 'str'
body: <p>I have a dataset with several tables, each in the form of countries, years, and some indicators. I have converted all the excel tables to csv files, then merged them into one table. </p>
  
  <p>The problem is that I have some tables that refuse to be merged, and the following message appears <code>TypeError: '&gt;' not supported between instances of 'int' and 'str'</code>
  I tried everything I can, but no luck, still the same error appears!
  Also, I tried with hundreds of different files, but there are still tens of files that face this problem.</p>
  
  <p>For the sample files <a href="https://docs.google.com/uc?id=1jUsbr5pw6sUMvewI4fmbpssroG4RZ7LE&amp;export=download" rel="nofollow noreferrer">file17.csv</a> and <a href="https://docs.google.com/uc?id=1Ufx6pvnSC6zQdTAj05ObmV027fA4-Mr3&amp;export=download" rel="nofollow noreferrer">file35.csv</a>  (In case someone needs to repeat it). <em>Here are the code I used:</em></p>
  
  <pre><code># To load the first file
  import pandas as pd
  filename1 = 'file17.csv'
  df1 = pd.read_csv(filename1, encoding='cp1252', low_memory=False)
  df1.set_index(['Country', 'Year'], inplace=True)
  df1.dropna(axis=0, how='all', inplace=True)
  df1.head()
  </code></pre>
  
  <p>Out>>></p>
  
  <pre><code>+-------------+------+--------+--------+
  |             |      | ind500 | ind356 |
  | Country     | Year |        |        |
  | Afghanistan | 1800 | 603.0  | NaN    |
  |             | 1801 | 603.0  | NaN    |
  |             | 1802 | 603.0  | NaN    |
  |             | 1803 | 603.0  | NaN    |
  |             | 1804 | 603.0  | NaN    |
  +-------------+------+--------+--------+
  </code></pre>
  
  <p>In>>></p>
  
  <pre><code># To load the second file
  filename2 = 'file35.csv'
  df2 = pd.read_csv(filename2, encoding='cp1252', low_memory=False)
  df2.set_index(['Country', 'Year'], inplace=True)
  df2.dropna(axis=0, how='all', inplace=True)
  df2.head()
  </code></pre>
  
  <p>Out>>></p>
  
  <p><a href="https://i.stack.imgur.com/hkx73.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hkx73.png" alt="enter image description here"></a></p>
  
  <pre><code># To merge the two dataframes
  gross_df = pd.merge(df1, df2, left_index=True, right_index=True, how='outer')
  gross_df.dropna(axis=0, how='all', inplace=True)
  print (gross_df.shape)
  gross_df.to_csv('merged.csv')
  </code></pre>
  
  <p><strong>Important notice:</strong> 
  I noticed that in all the successful files, the columns names appear in ascending orders i.e. <code>ind001</code>, <code>ind009</code>, <code>ind012</code>, as they were sorted automatically. while the files with errors have one or more columns with misordered placement like <code>ind500</code> followed by <code>in356</code> in the first table and the same applies to the second sample provided.</p>
  
  <p>Notice that the two dataframesindiceswo indices (<code>Country</code> and <code>year</code>)</p>
  
  <p><strong>The error</strong> </p>
  
  <pre><code>---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\algorithms.py in safe_sort(values, labels, na_sentinel, assume_unique)
      480         try:
  --&gt; 481             sorter = values.argsort()
      482             ordered = values.take(sorter)
  
  TypeError: '&gt;' not supported between instances of 'int' and 'str'
  
  During handling of the above exception, another exception occurred:
  
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-11-960b2698de60&gt; in &lt;module&gt;()
  ----&gt; 1 gross_df = pd.merge(df1, df2, left_index=True, right_index=True, how='outer', sort=False)
        2 gross_df.dropna(axis=0, how='all', inplace=True)
        3 print (gross_df.shape)
        4 gross_df.to_csv('merged.csv')
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\reshape\merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator)
       52                          right_index=right_index, sort=sort, suffixes=suffixes,
       53                          copy=copy, indicator=indicator)
  ---&gt; 54     return op.get_result()
       55 
       56 
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\reshape\merge.py in get_result(self)
      567                 self.left, self.right)
      568 
  --&gt; 569         join_index, left_indexer, right_indexer = self._get_join_info()
      570 
      571         ldata, rdata = self.left._data, self.right._data
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\reshape\merge.py in _get_join_info(self)
      720             join_index, left_indexer, right_indexer = \
      721                 left_ax.join(right_ax, how=self.how, return_indexers=True,
  --&gt; 722                              sort=self.sort)
      723         elif self.right_index and self.how == 'left':
      724             join_index, left_indexer, right_indexer = \
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\indexes\base.py in join(self, other, how, level, return_indexers, sort)
     2995             else:
     2996                 return self._join_non_unique(other, how=how,
  -&gt; 2997                                              return_indexers=return_indexers)
     2998         elif self.is_monotonic and other.is_monotonic:
     2999             try:
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\indexes\base.py in _join_non_unique(self, other, how, return_indexers)
     3076         left_idx, right_idx = _get_join_indexers([self.values],
     3077                                                  [other._values], how=how,
  -&gt; 3078                                                  sort=True)
     3079 
     3080         left_idx = _ensure_platform_int(left_idx)
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\reshape\merge.py in _get_join_indexers(left_keys, right_keys, sort, how, **kwargs)
      980 
      981     # get left &amp; right join labels and num. of levels at each location
  --&gt; 982     llab, rlab, shape = map(list, zip(* map(fkeys, left_keys, right_keys)))
      983 
      984     # get flat i8 keys from label lists
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\reshape\merge.py in _factorize_keys(lk, rk, sort)
     1409     if sort:
     1410         uniques = rizer.uniques.to_array()
  -&gt; 1411         llab, rlab = _sort_labels(uniques, llab, rlab)
     1412 
     1413     # NA group
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\reshape\merge.py in _sort_labels(uniques, left, right)
     1435     labels = np.concatenate([left, right])
     1436 
  -&gt; 1437     _, new_labels = algos.safe_sort(uniques, labels, na_sentinel=-1)
     1438     new_labels = _ensure_int64(new_labels)
     1439     new_left, new_right = new_labels[:l], new_labels[l:]
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\algorithms.py in safe_sort(values, labels, na_sentinel, assume_unique)
      483         except TypeError:
      484             # try this anyway
  --&gt; 485             ordered = sort_mixed(values)
      486 
      487     # labels:
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\pandas\core\algorithms.py in sort_mixed(values)
      469         str_pos = np.array([isinstance(x, string_types) for x in values],
      470                            dtype=bool)
  --&gt; 471         nums = np.sort(values[~str_pos])
      472         strs = np.sort(values[str_pos])
      473         return _ensure_object(np.concatenate([nums, strs]))
  
  C:\ProgramData\Anaconda2\envs\conda_python3\lib\site-packages\numpy\core\fromnumeric.py in sort(a, axis, kind, order)
      820     else:
      821         a = asanyarray(a).copy(order="K")
  --&gt; 822     a.sort(axis=axis, kind=kind, order=order)
      823     return a
      824 
  
  TypeError: '&gt;' not supported between instances of 'int' and 'str'
  </code></pre>