{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Examples**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" toy | \n",
" born | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Superman | \n",
" NaN | \n",
" NaT | \n",
"
\n",
" \n",
" 1 | \n",
" Batman | \n",
" Batmobile | \n",
" 1956-06-26 | \n",
"
\n",
" \n",
" 2 | \n",
" Spiderman | \n",
" Spiderman toy | \n",
" NaT | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name toy born\n",
"0 Superman NaN NaT\n",
"1 Batman Batmobile 1956-06-26\n",
"2 Spiderman Spiderman toy NaT"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame({\"name\": ['Superman', 'Batman', 'Spiderman'],\n",
" \"toy\": [np.nan, 'Batmobile', 'Spiderman toy'],\n",
" \"born\": [pd.NaT, pd.Timestamp(\"1956-06-26\"),\n",
" pd.NaT]})\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Drop the rows where at least one element is missing:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" toy | \n",
" born | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" Batman | \n",
" Batmobile | \n",
" 1956-06-26 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name toy born\n",
"1 Batman Batmobile 1956-06-26"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dropna()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Drop the columns where at least one element is missing:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Superman | \n",
"
\n",
" \n",
" 1 | \n",
" Batman | \n",
"
\n",
" \n",
" 2 | \n",
" Spiderman | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name\n",
"0 Superman\n",
"1 Batman\n",
"2 Spiderman"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dropna(axis='columns')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Drop the rows where all elements are missing."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" toy | \n",
" born | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Superman | \n",
" NaN | \n",
" NaT | \n",
"
\n",
" \n",
" 1 | \n",
" Batman | \n",
" Batmobile | \n",
" 1956-06-26 | \n",
"
\n",
" \n",
" 2 | \n",
" Spiderman | \n",
" Spiderman toy | \n",
" NaT | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name toy born\n",
"0 Superman NaN NaT\n",
"1 Batman Batmobile 1956-06-26\n",
"2 Spiderman Spiderman toy NaT"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dropna(how='all')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Keep only the rows with at least 2 non-NA values:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" toy | \n",
" born | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" Batman | \n",
" Batmobile | \n",
" 1956-06-26 | \n",
"
\n",
" \n",
" 2 | \n",
" Spiderman | \n",
" Spiderman toy | \n",
" NaT | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name toy born\n",
"1 Batman Batmobile 1956-06-26\n",
"2 Spiderman Spiderman toy NaT"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dropna(thresh=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define in which columns to look for missing values:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" toy | \n",
" born | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" Batman | \n",
" Batmobile | \n",
" 1956-06-26 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name toy born\n",
"1 Batman Batmobile 1956-06-26"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dropna(subset=['name', 'born'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Keep the DataFrame with valid entries in the same variable:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" toy | \n",
" born | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" Batman | \n",
" Batmobile | \n",
" 1956-06-26 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name toy born\n",
"1 Batman Batmobile 1956-06-26"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dropna(inplace=True)\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}