# Pandas Cheatsheet



# Cheatsheet 1

In **pandas**, there are a few quick + reliable ways to check the **data type of columns**, depending on what you need.

---

#### 1️⃣ Check *all* column types (most common)

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-df.dtypes"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`df.dtypes`</div></div>**Output example**

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-age-int64-height-flo"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"></div></div></div><div class="overflow-y-auto p-4" dir="ltr">`age         int64height    float64<span class="hljs-type">name</span>       <span class="hljs-keyword">object</span>active       <span class="hljs-type">bool</span><span class="hljs-type">date</span>     datetime64[ns]dtype: <span class="hljs-keyword">object</span>`</div></div>---

#### 2️⃣ Detailed overview (very useful for debugging)

<div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9" id="bkmrk--2"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr" id="bkmrk-df.info%28%29">`df.info()`</div>
Shows:

- column name
- non-null count
- dtype
- memory usage

---

#### 3️⃣ Check type of a *single column*

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-df%5B%22age%22%5D.dtype"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`df[<span class="hljs-string">"age"</span>].dtype`</div></div>or

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-df.age.dtype"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`df.age.dtype`</div></div>---

#### 4️⃣ Check only numeric / non-numeric columns

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-df.select_dtypes%28inc"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`df.select_dtypes(include=<span class="hljs-string">"number"</span>).columns`</div></div><div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-df.select_dtypes%28exc"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`df.select_dtypes(exclude=<span class="hljs-string">"number"</span>).columns`</div></div>---

#### 5️⃣ Check if a column is a specific type

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-import-pandas-as-pd-"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pdpd.api.types.is_numeric_dtype(df[<span class="hljs-string">"age"</span>])pd.api.types.is_string_dtype(df[<span class="hljs-string">"name"</span>])pd.api.types.is_datetime64_any_dtype(df[<span class="hljs-string">"date"</span>])pd.api.types.is_bool_dtype(df[<span class="hljs-string">"active"</span>])`</div></div>---

#### 6️⃣ Count how many columns per dtype

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-df.dtypes.value_coun"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`df.dtypes.value_counts()`</div></div>---

#### 7️⃣ Detect mixed or problematic types (object columns)

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-df.select_dtypes%28inc-1"><div class="sticky top-[calc(--spacing(9)+var(--header-height))] @w-xl/main:top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`df.select_dtypes(include=<span class="hljs-string">"object"</span>).apply(    <span class="hljs-keyword">lambda</span> s: s.<span class="hljs-built_in">map</span>(<span class="hljs-built_in">type</span>).value_counts())`</div></div>Useful when:

- CSV imported badly
- numbers stored as strings
- mixed types in one column

<table id="bkmrk-task-command-see-all" style="height: 203px; width: 76.6667%;"><thead><tr><th style="width: 49.9195%;">Task</th><th style="width: 49.9195%;">Command</th></tr></thead><tbody><tr><td style="width: 49.9195%;">See all dtypes</td><td style="width: 49.9195%;">`df.dtypes`</td></tr><tr><td style="width: 49.9195%;">Full summary</td><td style="width: 49.9195%;">`df.info()`</td></tr><tr><td style="width: 49.9195%;">Single column</td><td style="width: 49.9195%;">`df[col].dtype`</td></tr><tr><td style="width: 49.9195%;">Only numeric</td><td style="width: 49.9195%;">`select_dtypes()`</td></tr><tr><td style="width: 49.9195%;">Type check</td><td style="width: 49.9195%;">`pd.api.types.is_*`

</td></tr></tbody></table>