{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 第3章 pandasでデータを処理しよう\n",
"\n",
"### 3-4: DataFrame"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" r1 | \n",
" 1 | \n",
" 10 | \n",
" 100 | \n",
"
\n",
" \n",
" r2 | \n",
" 2 | \n",
" 20 | \n",
" 200 | \n",
"
\n",
" \n",
" r3 | \n",
" 3 | \n",
" 30 | \n",
" 300 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3\n",
"r1 1 10 100\n",
"r2 2 20 200\n",
"r3 3 30 300"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.1:DataFrameの作成\n",
"import pandas as pd\n",
"\n",
"df = pd.DataFrame(\n",
" [[1, 10, 100], [2, 20, 200], [3, 30, 300]],\n",
" index=[\"r1\", \"r2\", \"r3\"],\n",
" columns=[\"c1\", \"c2\", \"c3\"],\n",
")\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.3:ラベル指定\n",
"df.loc[\"r2\", \"c2\"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"c1 2\n",
"c2 20\n",
"c3 200\n",
"Name: r2, dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.5:すべての列ラベルを指定\n",
"df.loc[\"r2\", :]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"r1 10\n",
"r2 20\n",
"r3 30\n",
"Name: c2, dtype: int64"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.7:すべての行ラベルを指定\n",
"df.loc[:, \"c2\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" r1 | \n",
" 10 | \n",
" 100 | \n",
"
\n",
" \n",
" r3 | \n",
" 30 | \n",
" 300 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c2 c3\n",
"r1 10 100\n",
"r3 30 300"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.9:行ラベルをリストで指定し列ラベルをスライスで指定した抽出\n",
"# 行ラベルをリストで指定、 列ラベルをスライスで指定\n",
"df.loc[[\"r1\", \"r3\"], \"c2\":\"c3\"]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" r2 | \n",
" 2 | \n",
" 200 | \n",
"
\n",
" \n",
" r3 | \n",
" 3 | \n",
" 300 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c3\n",
"r2 2 200\n",
"r3 3 300"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.11:行の位置をスライスで指定し、 列の位置をリストで指定した抽出\n",
"df.iloc[1:3, [0, 2]]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"r1 10\n",
"r2 20\n",
"r3 30\n",
"Name: c2, dtype: int64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.13:[]による指定\n",
"df[\"c2\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" r1 | \n",
" False | \n",
" False | \n",
" True | \n",
"
\n",
" \n",
" r2 | \n",
" False | \n",
" True | \n",
" True | \n",
"
\n",
" \n",
" r3 | \n",
" False | \n",
" True | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3\n",
"r1 False False True\n",
"r2 False True True\n",
"r3 False True True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.15:DataFrameに対する比較演算\n",
"df > 10"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" r2 | \n",
" 2 | \n",
" 20 | \n",
" 200 | \n",
"
\n",
" \n",
" r3 | \n",
" 3 | \n",
" 30 | \n",
" 300 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3\n",
"r2 2 20 200\n",
"r3 3 30 300"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.17:比較演算を利用したデータの抽出\n",
"# c2列の値が10より大きいデータ\n",
"df.loc[df[\"c2\"] > 10]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" r2 | \n",
" 2 | \n",
" 20 | \n",
" 200 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3\n",
"r2 2 20 200"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# リスト3.4.19:複数条件を組み合わせたデータの抽出\n",
"# c1列が1より大きくかつ、c3列が300より小さいデータ\n",
"df.loc[(df[\"c1\"] > 1) & (df[\"c3\"] < 300)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}