title: Detect which columns are categorical in a dataframe with Python
body: <p>I want to build an algorithm that can detect which columns are categorical in a dataframe and which are numerical.</p>
  <p>Let's have a look at this dataset (just as an example) :</p>
  <pre><code>df = pd.DataFrame({&quot;ID&quot;: [12324, 26342, 62438], &quot;passengerClass&quot;: [1, 2, 2], &quot;nationality&quot;: [&quot;FR&quot;, &quot;ES&quot;, &quot;US&quot;]})
  </code></pre>
  <p>I can assume that categorical data are object/category types :</p>
  <pre><code>df.dtypes()
  </code></pre>
  <p>As we can see the &quot;nationality&quot; column is detected as an object type which is great.
  The problem is that the &quot;ID&quot; column and the &quot;passengerClass&quot; are detected as int64 type but are categorical.</p>
  <p>Is there a way to detect that these columns are also categorical?
  (I also thought about unique values but if we measure the speed of lots of cars it won't be the same at any time. Same for increasing values because sometimes we can delete rows and the ids won't be in the right order)</p>
  <p>Zero proposed that: <a href="https://stackoverflow.com/a/29803290/13919003">https://stackoverflow.com/a/29803290/13919003</a>
  But in his answer, he doesn't care about int or float being categorical columns which is the case in the « passengerClass » column.</p>