Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
カラムを指定してソート
{
"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(\"1ACB\"))"
]
},
{
"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>1</th>\n",
" <th>A</th>\n",
" <th>C</th>\n",
" <th>B</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2018-01-01</th>\n",
" <td>0.191224</td>\n",
" <td>0.011435</td>\n",
" <td>-0.985425</td>\n",
" <td>-0.762398</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-02</th>\n",
" <td>-1.744498</td>\n",
" <td>0.003786</td>\n",
" <td>-0.213510</td>\n",
" <td>-1.186373</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-03</th>\n",
" <td>1.249193</td>\n",
" <td>0.685888</td>\n",
" <td>-0.592306</td>\n",
" <td>1.183879</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-04</th>\n",
" <td>0.075220</td>\n",
" <td>0.432484</td>\n",
" <td>-0.880817</td>\n",
" <td>0.640910</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-05</th>\n",
" <td>-0.956537</td>\n",
" <td>0.272183</td>\n",
" <td>1.201892</td>\n",
" <td>1.083639</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-06</th>\n",
" <td>0.068607</td>\n",
" <td>-0.509204</td>\n",
" <td>0.660314</td>\n",
" <td>0.096451</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-07</th>\n",
" <td>-0.687572</td>\n",
" <td>-1.136388</td>\n",
" <td>1.574985</td>\n",
" <td>-0.557982</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-08</th>\n",
" <td>-1.962972</td>\n",
" <td>-0.833125</td>\n",
" <td>-0.874113</td>\n",
" <td>-0.382513</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-09</th>\n",
" <td>-0.780895</td>\n",
" <td>-0.163033</td>\n",
" <td>-0.751909</td>\n",
" <td>0.331501</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-10</th>\n",
" <td>0.938139</td>\n",
" <td>0.378014</td>\n",
" <td>1.349160</td>\n",
" <td>1.240721</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 1 A C B\n",
"2018-01-01 0.191224 0.011435 -0.985425 -0.762398\n",
"2018-01-02 -1.744498 0.003786 -0.213510 -1.186373\n",
"2018-01-03 1.249193 0.685888 -0.592306 1.183879\n",
"2018-01-04 0.075220 0.432484 -0.880817 0.640910\n",
"2018-01-05 -0.956537 0.272183 1.201892 1.083639\n",
"2018-01-06 0.068607 -0.509204 0.660314 0.096451\n",
"2018-01-07 -0.687572 -1.136388 1.574985 -0.557982\n",
"2018-01-08 -1.962972 -0.833125 -0.874113 -0.382513\n",
"2018-01-09 -0.780895 -0.163033 -0.751909 0.331501\n",
"2018-01-10 0.938139 0.378014 1.349160 1.240721"
]
},
"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>1</th>\n",
" <th>A</th>\n",
" <th>C</th>\n",
" <th>B</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2018-01-02</th>\n",
" <td>-1.744498</td>\n",
" <td>0.003786</td>\n",
" <td>-0.213510</td>\n",
" <td>-1.186373</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-01</th>\n",
" <td>0.191224</td>\n",
" <td>0.011435</td>\n",
" <td>-0.985425</td>\n",
" <td>-0.762398</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-07</th>\n",
" <td>-0.687572</td>\n",
" <td>-1.136388</td>\n",
" <td>1.574985</td>\n",
" <td>-0.557982</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-08</th>\n",
" <td>-1.962972</td>\n",
" <td>-0.833125</td>\n",
" <td>-0.874113</td>\n",
" <td>-0.382513</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-06</th>\n",
" <td>0.068607</td>\n",
" <td>-0.509204</td>\n",
" <td>0.660314</td>\n",
" <td>0.096451</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-09</th>\n",
" <td>-0.780895</td>\n",
" <td>-0.163033</td>\n",
" <td>-0.751909</td>\n",
" <td>0.331501</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-04</th>\n",
" <td>0.075220</td>\n",
" <td>0.432484</td>\n",
" <td>-0.880817</td>\n",
" <td>0.640910</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-05</th>\n",
" <td>-0.956537</td>\n",
" <td>0.272183</td>\n",
" <td>1.201892</td>\n",
" <td>1.083639</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-03</th>\n",
" <td>1.249193</td>\n",
" <td>0.685888</td>\n",
" <td>-0.592306</td>\n",
" <td>1.183879</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2018-01-10</th>\n",
" <td>0.938139</td>\n",
" <td>0.378014</td>\n",
" <td>1.349160</td>\n",
" <td>1.240721</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 1 A C B\n",
"2018-01-02 -1.744498 0.003786 -0.213510 -1.186373\n",
"2018-01-01 0.191224 0.011435 -0.985425 -0.762398\n",
"2018-01-07 -0.687572 -1.136388 1.574985 -0.557982\n",
"2018-01-08 -1.962972 -0.833125 -0.874113 -0.382513\n",
"2018-01-06 0.068607 -0.509204 0.660314 0.096451\n",
"2018-01-09 -0.780895 -0.163033 -0.751909 0.331501\n",
"2018-01-04 0.075220 0.432484 -0.880817 0.640910\n",
"2018-01-05 -0.956537 0.272183 1.201892 1.083639\n",
"2018-01-03 1.249193 0.685888 -0.592306 1.183879\n",
"2018-01-10 0.938139 0.378014 1.349160 1.240721"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# カラムを指定してソートする\n",
"df.sort_values(by=\"B\")"
]
},
{
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment