{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "taruma_hk60_resample", "version": "0.3.2", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "f7sSLaY7hEV5", "colab_type": "text" }, "source": [ "Berdasarkan isu [#60](https://github.com/taruma/hidrokit/issues/60): __Menentukan kriteria berdasarkan total nilai bulanan__\n", "\n", "Deskripsi permasalahan:\n", "- Saya memiliki __dataset harian__ nilai a, b, c selama 50 tahun.\n", "- Saya ingin mengkategorikan nilai di kolom a, __berdasarkan__ nilai total bulanan-nya.\n", "- Hasil pengategorian tersebut dibuat pada kolom baru bernama 'kategori'." ] }, { "cell_type": "code", "metadata": { "id": "cn82ATqtg5FL", "colab_type": "code", "colab": {} }, "source": [ "# import library\n", "import pandas as pd\n", "import numpy as np" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "MoRqkKdnhUFd", "colab_type": "code", "outputId": "1d3cf757-b39c-47fe-826f-4d323058b595", "colab": { "base_uri": "https://localhost:8080/", "height": 170 } }, "source": [ "# buat dataset random\n", "SEED = 110891\n", "np.random.seed(SEED)\n", "date_index = pd.date_range('20000101', '20501231')\n", "dataset = pd.DataFrame(\n", " index=date_index,\n", " data=np.random.rand(len(date_index), 3)*10,\n", " columns='a b c'.split()\n", ")\n", "dataset.info()" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "\n", "DatetimeIndex: 18628 entries, 2000-01-01 to 2050-12-31\n", "Freq: D\n", "Data columns (total 3 columns):\n", "a 18628 non-null float64\n", "b 18628 non-null float64\n", "c 18628 non-null float64\n", "dtypes: float64(3)\n", "memory usage: 582.1 KB\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "WqdHuxfphyI8", "colab_type": "code", "outputId": "fe12e39a-364d-4429-ee32-169dea51fb64", "colab": { "base_uri": "https://localhost:8080/", "height": 204 } }, "source": [ "dataset.head()" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abc
2000-01-012.8777663.2133782.609509
2000-01-026.0870790.5288142.181410
2000-01-037.7885538.5975643.401175
2000-01-047.9524443.1823241.600600
2000-01-051.7213167.6185915.018759
\n", "
" ], "text/plain": [ " a b c\n", "2000-01-01 2.877766 3.213378 2.609509\n", "2000-01-02 6.087079 0.528814 2.181410\n", "2000-01-03 7.788553 8.597564 3.401175\n", "2000-01-04 7.952444 3.182324 1.600600\n", "2000-01-05 1.721316 7.618591 5.018759" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "code", "metadata": { "id": "WQJzzyVsqiC9", "colab_type": "code", "outputId": "c9d6ca08-6fda-4e38-b47e-11acbb869dea", "colab": { "base_uri": "https://localhost:8080/", "height": 1000 } }, "source": [ "# menghitung total ch setiap bulannya\n", "total_summary = dataset.groupby([dataset.index.year, dataset.index.month]).sum()\n", "total_summary" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abc
20001162.847727156.897045144.812644
2146.883002163.938908125.541164
3149.917733162.343038153.770483
4151.332312146.398578131.572011
5148.824870159.312776178.204217
6142.645200141.804644162.177388
7170.140213130.409430191.273702
8170.068540159.861622179.001989
9178.699585157.825042144.558428
10145.530305191.522354178.904414
11128.861376152.381631121.446012
12128.531776145.206447160.007800
20011165.739167150.058786160.826603
2141.511927134.187962130.801650
3135.688425177.826575173.841181
4167.878367163.536799167.083027
5159.875967168.663616120.514476
6155.665123153.639577153.308357
7142.746376139.065767147.011501
8142.728787174.440000148.623053
9116.060098165.032239153.688107
10168.868071164.222337170.667792
11140.969798155.106032121.242189
12123.714519163.179190166.616499
20021167.053233155.069400171.605847
2120.012374146.039734153.264692
3183.218227164.571052154.357133
4154.822616120.226232102.963488
5169.889488152.184870142.519123
6151.163008130.519755151.762828
...............
20487148.672508167.142954164.381128
8126.655985148.710034144.413049
9183.513775114.640279169.897208
10135.829907151.918643176.459592
11146.398529125.322861148.996056
12159.041660156.619241165.230088
20491157.052500139.911295145.751750
2117.204951135.909696128.467320
3140.775834172.049295186.388653
4142.940225156.801915141.438030
5175.040427157.488158156.365536
6140.708484133.383572147.509383
7145.790755165.487569154.127876
8149.804165169.480737138.583017
9158.301834164.360739164.492783
10159.818715141.081036142.240236
1196.027429154.389690154.491933
12168.802017140.951503148.711046
20501175.387880156.662521203.628996
2137.018246133.811524144.568716
3161.716170143.697188143.519191
4162.027600135.305125156.008888
5154.067237161.674996169.782035
6153.781478158.463390171.016871
7149.692955171.071892144.742615
8163.018816161.417455157.176567
9142.620717171.070855117.322726
10138.459144137.613439164.343386
11150.625937147.692128164.288570
12157.485470184.238850160.089807
\n", "

612 rows × 3 columns

\n", "
" ], "text/plain": [ " a b c\n", "2000 1 162.847727 156.897045 144.812644\n", " 2 146.883002 163.938908 125.541164\n", " 3 149.917733 162.343038 153.770483\n", " 4 151.332312 146.398578 131.572011\n", " 5 148.824870 159.312776 178.204217\n", " 6 142.645200 141.804644 162.177388\n", " 7 170.140213 130.409430 191.273702\n", " 8 170.068540 159.861622 179.001989\n", " 9 178.699585 157.825042 144.558428\n", " 10 145.530305 191.522354 178.904414\n", " 11 128.861376 152.381631 121.446012\n", " 12 128.531776 145.206447 160.007800\n", "2001 1 165.739167 150.058786 160.826603\n", " 2 141.511927 134.187962 130.801650\n", " 3 135.688425 177.826575 173.841181\n", " 4 167.878367 163.536799 167.083027\n", " 5 159.875967 168.663616 120.514476\n", " 6 155.665123 153.639577 153.308357\n", " 7 142.746376 139.065767 147.011501\n", " 8 142.728787 174.440000 148.623053\n", " 9 116.060098 165.032239 153.688107\n", " 10 168.868071 164.222337 170.667792\n", " 11 140.969798 155.106032 121.242189\n", " 12 123.714519 163.179190 166.616499\n", "2002 1 167.053233 155.069400 171.605847\n", " 2 120.012374 146.039734 153.264692\n", " 3 183.218227 164.571052 154.357133\n", " 4 154.822616 120.226232 102.963488\n", " 5 169.889488 152.184870 142.519123\n", " 6 151.163008 130.519755 151.762828\n", "... ... ... ...\n", "2048 7 148.672508 167.142954 164.381128\n", " 8 126.655985 148.710034 144.413049\n", " 9 183.513775 114.640279 169.897208\n", " 10 135.829907 151.918643 176.459592\n", " 11 146.398529 125.322861 148.996056\n", " 12 159.041660 156.619241 165.230088\n", "2049 1 157.052500 139.911295 145.751750\n", " 2 117.204951 135.909696 128.467320\n", " 3 140.775834 172.049295 186.388653\n", " 4 142.940225 156.801915 141.438030\n", " 5 175.040427 157.488158 156.365536\n", " 6 140.708484 133.383572 147.509383\n", " 7 145.790755 165.487569 154.127876\n", " 8 149.804165 169.480737 138.583017\n", " 9 158.301834 164.360739 164.492783\n", " 10 159.818715 141.081036 142.240236\n", " 11 96.027429 154.389690 154.491933\n", " 12 168.802017 140.951503 148.711046\n", "2050 1 175.387880 156.662521 203.628996\n", " 2 137.018246 133.811524 144.568716\n", " 3 161.716170 143.697188 143.519191\n", " 4 162.027600 135.305125 156.008888\n", " 5 154.067237 161.674996 169.782035\n", " 6 153.781478 158.463390 171.016871\n", " 7 149.692955 171.071892 144.742615\n", " 8 163.018816 161.417455 157.176567\n", " 9 142.620717 171.070855 117.322726\n", " 10 138.459144 137.613439 164.343386\n", " 11 150.625937 147.692128 164.288570\n", " 12 157.485470 184.238850 160.089807\n", "\n", "[612 rows x 3 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] }, { "cell_type": "code", "metadata": { "id": "J54gY_fKjDGg", "colab_type": "code", "outputId": "797a17fc-cb3e-46b0-c669-8c33918a7d8b", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "source": [ "# Memilih nilai spesifik bulan dan tahun pada kolom tertentu\n", "TAHUN, BULAN, KOLOM = 2000, 1, 'a'\n", "total_summary.loc[(TAHUN, BULAN), KOLOM]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "162.84772662835172" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "code", "metadata": { "id": "LmiD9JurjS5L", "colab_type": "code", "colab": {} }, "source": [ "# \n", "\n", "def total_each_month(x, column='ch', summary=None):\n", " year = x['index'].year\n", " month = x['index'].month\n", " return summary.loc[(year, month), column]\n", "\n", "def criteria(value):\n", " if value < 130:\n", " return \"rendah\"\n", " elif value >= 130 and value <= 160:\n", " return \"sedang\"\n", " else:\n", " return \"tinggi\"\n", "\n", "KOLOM = 'a'\n", " \n", "dataset['total_a'] = dataset.reset_index().apply(lambda x: total_each_month(x, column=KOLOM, summary=total_summary), axis=1).values\n", "dataset['kategori'] = dataset['total_a'].apply(criteria)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "v7iE2vvC2TSu", "colab_type": "code", "outputId": "e9c09b23-974f-4875-9e86-e6a8ced3961f", "colab": { "base_uri": "https://localhost:8080/", "height": 204 } }, "source": [ "dataset.head()" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abctotal_akategori
2000-01-012.8777663.2133782.609509162.847727tinggi
2000-01-026.0870790.5288142.181410162.847727tinggi
2000-01-037.7885538.5975643.401175162.847727tinggi
2000-01-047.9524443.1823241.600600162.847727tinggi
2000-01-051.7213167.6185915.018759162.847727tinggi
\n", "
" ], "text/plain": [ " a b c total_a kategori\n", "2000-01-01 2.877766 3.213378 2.609509 162.847727 tinggi\n", "2000-01-02 6.087079 0.528814 2.181410 162.847727 tinggi\n", "2000-01-03 7.788553 8.597564 3.401175 162.847727 tinggi\n", "2000-01-04 7.952444 3.182324 1.600600 162.847727 tinggi\n", "2000-01-05 1.721316 7.618591 5.018759 162.847727 tinggi" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "code", "metadata": { "id": "ZrisfWzl2LSn", "colab_type": "code", "outputId": "49283388-6ed6-4e63-9324-ab18107a5e0b", "colab": { "base_uri": "https://localhost:8080/", "height": 359 } }, "source": [ "dataset.sample(n=10, random_state=SEED)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abctotal_akategori
2048-11-111.2238140.4365035.906254146.398529sedang
2006-04-057.4941723.3345111.982660161.010925tinggi
2038-06-191.7374481.2666652.950612130.366532sedang
2022-06-147.9991641.0794374.436997171.157052tinggi
2012-10-249.0060641.9422240.583746160.424030tinggi
2007-06-100.7906716.9431351.963164139.102825sedang
2008-03-317.3622423.0706369.000031143.855427sedang
2004-08-206.6968452.6765293.607975179.573750tinggi
2014-12-281.0353548.8117850.589958143.172699sedang
2044-07-113.6865248.4405040.009692151.676157sedang
\n", "