Skip to content

Instantly share code, notes, and snippets.

@pybokeh
Created June 18, 2018 19:00
Show Gist options
  • Select an option

  • Save pybokeh/e0ca9bd2c48903f6e3b187bc10902883 to your computer and use it in GitHub Desktop.

Select an option

Save pybokeh/e0ca9bd2c48903f6e3b187bc10902883 to your computer and use it in GitHub Desktop.
pandas/Untitled.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import pandas as pd",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df = pd.read_excel(r'D:\\temp\\sample.xlsx', index_col=0)",
"execution_count": 15,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df",
"execution_count": 16,
"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>value</th>\n </tr>\n <tr>\n <th>date</th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2013-03-31 22:07:48</th>\n <td>93.25</td>\n </tr>\n <tr>\n <th>2013-03-31 22:07:49</th>\n <td>93.30</td>\n </tr>\n <tr>\n <th>2013-03-31 22:08:13</th>\n <td>100.00</td>\n </tr>\n <tr>\n <th>2014-03-31 22:08:14</th>\n <td>100.00</td>\n </tr>\n <tr>\n <th>2014-03-31 22:08:16</th>\n <td>100.00</td>\n </tr>\n <tr>\n <th>2014-03-31 22:08:19</th>\n <td>100.00</td>\n </tr>\n <tr>\n <th>2015-03-31 22:08:21</th>\n <td>100.00</td>\n </tr>\n <tr>\n <th>2015-03-31 22:08:25</th>\n <td>100.00</td>\n </tr>\n <tr>\n <th>2015-03-31 22:08:29</th>\n <td>100.00</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " value\ndate \n2013-03-31 22:07:48 93.25\n2013-03-31 22:07:49 93.30\n2013-03-31 22:08:13 100.00\n2014-03-31 22:08:14 100.00\n2014-03-31 22:08:16 100.00\n2014-03-31 22:08:19 100.00\n2015-03-31 22:08:21 100.00\n2015-03-31 22:08:25 100.00\n2015-03-31 22:08:29 100.00"
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "min_year = df.index.min().year\nmax_year = df.index.max().year\nstr_years = [str(year) for year in range(min_year, max_year + 1)]",
"execution_count": 30,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "str_years",
"execution_count": 31,
"outputs": [
{
"data": {
"text/plain": "['2013', '2014', '2015']"
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dataframe_list_by_year = [df[year] for year in str_years]",
"execution_count": 40,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dataframe_list_by_year",
"execution_count": 41,
"outputs": [
{
"data": {
"text/plain": "[ value\n date \n 2013-03-31 22:07:48 93.25\n 2013-03-31 22:07:49 93.30\n 2013-03-31 22:08:13 100.00, value\n date \n 2014-03-31 22:08:14 100.0\n 2014-03-31 22:08:16 100.0\n 2014-03-31 22:08:19 100.0, value\n date \n 2015-03-31 22:08:21 100.0\n 2015-03-31 22:08:25 100.0\n 2015-03-31 22:08:29 100.0]"
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "len(dataframe_list)",
"execution_count": 34,
"outputs": [
{
"data": {
"text/plain": "3"
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "monthly_index = [date.strftime(\"%Y-%m\") for date in df.resample('M').count().index]",
"execution_count": 53,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "monthly_index",
"execution_count": 54,
"outputs": [
{
"data": {
"text/plain": "['2013-03',\n '2013-04',\n '2013-05',\n '2013-06',\n '2013-07',\n '2013-08',\n '2013-09',\n '2013-10',\n '2013-11',\n '2013-12',\n '2014-01',\n '2014-02',\n '2014-03',\n '2014-04',\n '2014-05',\n '2014-06',\n '2014-07',\n '2014-08',\n '2014-09',\n '2014-10',\n '2014-11',\n '2014-12',\n '2015-01',\n '2015-02',\n '2015-03']"
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dataframe_list_by_month = [df[month] for month in monthly_index]",
"execution_count": 55,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df['2015-03']",
"execution_count": 58,
"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>value</th>\n </tr>\n <tr>\n <th>date</th>\n <th></th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2015-03-31 22:08:21</th>\n <td>100.0</td>\n </tr>\n <tr>\n <th>2015-03-31 22:08:25</th>\n <td>100.0</td>\n </tr>\n <tr>\n <th>2015-03-31 22:08:29</th>\n <td>100.0</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " value\ndate \n2015-03-31 22:08:21 100.0\n2015-03-31 22:08:25 100.0\n2015-03-31 22:08:29 100.0"
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dataframe_list_by_month",
"execution_count": 56,
"outputs": [
{
"data": {
"text/plain": "[ value\n date \n 2013-03-31 22:07:48 93.25\n 2013-03-31 22:07:49 93.30\n 2013-03-31 22:08:13 100.00, Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], value\n date \n 2014-03-31 22:08:14 100.0\n 2014-03-31 22:08:16 100.0\n 2014-03-31 22:08:19 100.0, Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], Empty DataFrame\n Columns: [value]\n Index: [], value\n date \n 2015-03-31 22:08:21 100.0\n 2015-03-31 22:08:25 100.0\n 2015-03-31 22:08:29 100.0]"
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "pandas/Untitled.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment