Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active January 26, 2018 13:48
Show Gist options
  • Save kurozumi/ff1332d2445ef4a431013303db036fac to your computer and use it in GitHub Desktop.
Save kurozumi/ff1332d2445ef4a431013303db036fac to your computer and use it in GitHub Desktop.
行列のデータフレームの先頭3件取得
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# index作成\n",
"dates = pd.date_range('20180101', periods=10)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# 行列のデータフレーム作成\n",
"df = pd.DataFrame(np.random.randn(10,4), index=dates, columns=list(\"ABCD\"))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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>A</th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" <th>D</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2018-01-01</th>\n",
" <td>0.231544</td>\n",
" <td>1.238792</td>\n",
" <td>-0.912333</td>\n",
" <td>-2.151868</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-02</th>\n",
" <td>-0.195222</td>\n",
" <td>-0.167689</td>\n",
" <td>0.273792</td>\n",
" <td>0.363077</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-03</th>\n",
" <td>-1.204195</td>\n",
" <td>-0.517626</td>\n",
" <td>0.566952</td>\n",
" <td>-0.036471</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-04</th>\n",
" <td>-1.029636</td>\n",
" <td>0.132427</td>\n",
" <td>2.245746</td>\n",
" <td>0.820056</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-05</th>\n",
" <td>-0.019373</td>\n",
" <td>0.362207</td>\n",
" <td>-1.103825</td>\n",
" <td>-1.489115</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-06</th>\n",
" <td>1.000017</td>\n",
" <td>0.185084</td>\n",
" <td>0.511161</td>\n",
" <td>1.342534</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-07</th>\n",
" <td>0.096106</td>\n",
" <td>-0.806579</td>\n",
" <td>-0.010164</td>\n",
" <td>-0.743488</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-08</th>\n",
" <td>0.359012</td>\n",
" <td>1.710643</td>\n",
" <td>-1.437037</td>\n",
" <td>0.797776</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-09</th>\n",
" <td>0.513066</td>\n",
" <td>0.071814</td>\n",
" <td>-0.319051</td>\n",
" <td>-1.235153</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-10</th>\n",
" <td>0.918525</td>\n",
" <td>0.422145</td>\n",
" <td>-0.139924</td>\n",
" <td>-1.332073</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" A B C D\n",
"2018-01-01 0.231544 1.238792 -0.912333 -2.151868\n",
"2018-01-02 -0.195222 -0.167689 0.273792 0.363077\n",
"2018-01-03 -1.204195 -0.517626 0.566952 -0.036471\n",
"2018-01-04 -1.029636 0.132427 2.245746 0.820056\n",
"2018-01-05 -0.019373 0.362207 -1.103825 -1.489115\n",
"2018-01-06 1.000017 0.185084 0.511161 1.342534\n",
"2018-01-07 0.096106 -0.806579 -0.010164 -0.743488\n",
"2018-01-08 0.359012 1.710643 -1.437037 0.797776\n",
"2018-01-09 0.513066 0.071814 -0.319051 -1.235153\n",
"2018-01-10 0.918525 0.422145 -0.139924 -1.332073"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# データフレーム表示\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>A</th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" <th>D</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2018-01-01</th>\n",
" <td>0.231544</td>\n",
" <td>1.238792</td>\n",
" <td>-0.912333</td>\n",
" <td>-2.151868</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-02</th>\n",
" <td>-0.195222</td>\n",
" <td>-0.167689</td>\n",
" <td>0.273792</td>\n",
" <td>0.363077</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-03</th>\n",
" <td>-1.204195</td>\n",
" <td>-0.517626</td>\n",
" <td>0.566952</td>\n",
" <td>-0.036471</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" A B C D\n",
"2018-01-01 0.231544 1.238792 -0.912333 -2.151868\n",
"2018-01-02 -0.195222 -0.167689 0.273792 0.363077\n",
"2018-01-03 -1.204195 -0.517626 0.566952 -0.036471"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 先頭の3件取得\n",
"df.head(3)"
]
}
],
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment