Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chetanambi/4bc8593a00ccc383f14c1c853b2c82b8 to your computer and use it in GitHub Desktop.
Save chetanambi/4bc8593a00ccc383f14c1c853b2c82b8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "dfb7c684",
"metadata": {},
"source": [
"# How to iterate over rows in a dataframe in Pandas"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "1968e636",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>year</th>\n",
" <th>month</th>\n",
" <th>passengers</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1949</td>\n",
" <td>Jan</td>\n",
" <td>112</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1949</td>\n",
" <td>Feb</td>\n",
" <td>118</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" year month passengers\n",
"0 1949 Jan 112\n",
"1 1949 Feb 118"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import seaborn as sns\n",
"df = sns.load_dataset('flights').head(2)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "cbd2dbdf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"| | year | month | passengers |\n",
"|---:|-------:|:--------|-------------:|\n",
"| 0 | 1949 | Jan | 112 |\n",
"| 1 | 1949 | Feb | 118 |\n"
]
}
],
"source": [
"print(df.to_markdown())"
]
},
{
"cell_type": "markdown",
"id": "f21c268b",
"metadata": {},
"source": [
"## iterrows() method"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8945e28a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for idx, row in df.iterrows():\n",
" print(row['year'], row['month'], row['passengers'])"
]
},
{
"cell_type": "markdown",
"id": "e2d70cb8",
"metadata": {},
"source": [
"## itertuples() method"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "325acbbd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pandas(Index=0, year=1949, month='Jan', passengers=112)\n",
"Pandas(Index=1, year=1949, month='Feb', passengers=118)\n"
]
}
],
"source": [
"for row in df.itertuples():\n",
" print(row)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4d02e9ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for row in df.itertuples():\n",
" print(row.year, row.month, row.passengers)"
]
},
{
"cell_type": "markdown",
"id": "07d7bb21",
"metadata": {},
"source": [
"## loc[] method"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0085c55e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for idx in range(len(df)):\n",
" print(df.loc[idx, \"year\"], df.loc[idx, \"month\"], df.loc[idx, 'passengers'])"
]
},
{
"cell_type": "markdown",
"id": "c575c7ed",
"metadata": {},
"source": [
"## iloc[] method"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4a436801",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for idx in range(len(df)):\n",
" print(df.iloc[idx, 0], df.iloc[idx, 1], df.iloc[idx, 2])"
]
},
{
"cell_type": "markdown",
"id": "9b23a8f8",
"metadata": {},
"source": [
"## index"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "54aab88b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for idx in df.index:\n",
" print(df['year'][idx], df['month'][idx], df['passengers'][idx])"
]
},
{
"cell_type": "markdown",
"id": "1e9a53f8",
"metadata": {},
"source": [
"## to_dict() method"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "73a06f14",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for item in df.to_dict(orient='records'):\n",
" print(item['year'], item['month'], item['passengers'])"
]
},
{
"cell_type": "markdown",
"id": "369be8be",
"metadata": {},
"source": [
"## to_numpy() method"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e6a910d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for item in df.to_numpy():\n",
" print(item[0], item[1], item[2])"
]
},
{
"cell_type": "markdown",
"id": "4e25b702",
"metadata": {},
"source": [
"## iteritems & items()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "32e663af",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for key, value in df.T.iteritems():\n",
" print(value.year, value.month, value.passengers)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "1093f638",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1949 Jan 112\n",
"1949 Feb 118\n"
]
}
],
"source": [
"for key, value in df.T.items():\n",
" print(value.year, value.month, value.passengers)"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment