diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96e67f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +**/.ipynb_checkpoints/ \ No newline at end of file diff --git a/api/requirements.txt b/api/requirements.txt index b50f2b8..7eb4314 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -5,4 +5,8 @@ boto3>=1.17.72 requests>=2.22.0 werkzeug<2.0.0 # werkzeug>=2.0.0 -pytest>=6.2.4 \ No newline at end of file +pytest>=6.2.4 +requests>=2.25.1 +beautifulsoup4>=4.9.3 +pandas>=1.2.4 +lxml>=4.6.3 \ No newline at end of file diff --git a/api/src/candidate.py b/api/src/candidate.py index ddf719f..7ffa4b7 100644 --- a/api/src/candidate.py +++ b/api/src/candidate.py @@ -1,7 +1,7 @@ from src import app from flask import jsonify, request -@app.route('/getCandidatesFromElection', methods['GET', 'POST']) +@app.route('/getCandidatesFromElection', methods=['GET', 'POST']) def getCandidatesFromElection(): # TODO: Implement the function return jsonify({'error':'Function not implemented'}) diff --git a/api/src/constituency.py b/api/src/constituency.py index 70fd0da..1fa7be4 100644 --- a/api/src/constituency.py +++ b/api/src/constituency.py @@ -21,7 +21,7 @@ def getConstituencyFromVoterId(): voter_id = request.json.get('voter_id') if request.method == 'POST' else request.args.get('voter_id') if not voter_id: return jsonify({'error':"request body must contain the field 'voter_id'"}), 400 - elif not isinstance(lat, str): + elif not isinstance(voter_id, str): return jsonify({'error':"field 'voter_id' must be of type str"}), 400 # TODO: Implement the function diff --git a/api/src/election.py b/api/src/election.py index 445e9d2..127ae46 100644 --- a/api/src/election.py +++ b/api/src/election.py @@ -1,12 +1,88 @@ +from datetime import date +import traceback +from typing import Tuple + +from werkzeug.exceptions import FailedDependency, Gone from src import app from flask import jsonify, request +import requests +import pandas as pd -@app.route('/getElectionsFromConstituency', methods['GET', 'POST']) +@app.route('/getElectionsFromConstituency', methods=['GET', 'POST']) def getElectionsFromConstituency(): - # TODO: Implement the function + # TODO: Implement the function return jsonify({'error':'Function not implemented'}) -@app.route('/getElectionsFromPartNo', methods['GET', 'POST']) +@app.route('/getElectionsFromPartNo', methods=['GET', 'POST']) def getElectionsFromPartNo(): - # TODO: Implement the function - return jsonify({'error':'Function not implemented'}) \ No newline at end of file + # TODO: Implement the function + return jsonify({'error':'Function not implemented'}) + +@app.route('/getElectionsFromState', methods=['GET', 'POST']) +def getElectionsFromState(): + state_elections, exp = __getNextElectionForState(request.args.get('state', '', type=str)) + if exp != None: + app.logger.error(''.join(traceback.format_exception(type(exp), exp, exp.__traceback__))) + return jsonify({'exception':exp.description}), exp.code + return jsonify(state_elections) + +@app.route('/getAllFutureElections', methods=['GET']) +def getAllFutureElections(): + r = requests.get('https://eci.gov.in/elections/future-elections/') + print("getAllFutureElections", r.status_code) + if r.status_code == 200: + table_val = __getTableInformationFromHTML(r.text) + data = [table.to_dict(orient='records') for table in table_val] + return jsonify(data) + else: + return jsonify({'error':'Could not reach Election Commission of India\'s website'}) + +def __getTableInformationFromHTML(html_text: str): + all_data = [] + tables = pd.read_html(html_text) + for table in tables: + if len(table) > 0: + all_data.append(table) + return all_data + +def __getNextElectionForState(stateName: str) -> Tuple[object, Exception]: + try: + r = requests.get('https://eci.gov.in/elections/term-of-houses/') + print("__getNextElectionForState", r.status_code) + if r.status_code != 200: + raise Gone("Failed to get Term-of-houses from ECI website", r.raw) + except Exception as e: + return None, e + + next_elections = [] + terms_of_houses = __getTableInformationFromHTML(r.text) + union_elections_terms = terms_of_houses[0] + state_elections_terms = terms_of_houses[1] + + # extract the date for next lok sabha elections + try: + lok_sabha_elections = union_elections_terms[(union_elections_terms['OFFICE/STATE'] == "LOK SABHA") & (union_elections_terms['TO'].str.contains('^\d+\.\d+\.\d+$'))] + if lok_sabha_elections.shape[0] != 1: + # there should be only one end-date for lok sabha elections + raise FailedDependency("Found "+ str(lok_sabha_elections.shape[0]) +" end-dates for the term of lok sabha on ECI website") + except Exception as e: + return None, e + next_elections.append(lok_sabha_elections.to_dict(orient='records')[0]) + + # extract the date for next state elections + try: + state_elections = state_elections_terms[(state_elections_terms['HOUSE/STATE'] == stateName.upper()) & (state_elections_terms['TO'].str.contains('^\d+\.\d+\.\d+$'))] + if state_elections.shape[0] != 1: + # there should be only one end-date for any state's elections + raise FailedDependency("Found "+ str(state_elections.shape[0]) +" end-dates for the term of " + stateName.upper() + " State on ECI website") + except Exception as e: + return None, e + next_elections.append(state_elections.to_dict(orient='records')[0]) + + next_elections.sort(key=lambda df: __getDateFromDateString(df['TO'])) + + return next_elections, None + +def __getDateFromDateString(date_string: str) -> date: + day, month, year = [int(k) for k in date_string.split('.')] + return date(year, month, day) \ No newline at end of file diff --git a/notebooks/next_elections.ipynb b/notebooks/next_elections.ipynb new file mode 100644 index 0000000..80c361a --- /dev/null +++ b/notebooks/next_elections.ipynb @@ -0,0 +1,364 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "from bs4 import BeautifulSoup as bs\n", + "import xmltodict\n", + "import json\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "200\n" + ] + } + ], + "source": [ + "# r = requests.get('https://eci.gov.in/elections/future-elections/')\n", + "r = requests.get('https://eci.gov.in/elections/term-of-houses/')\n", + "print(r.status_code)\n", + "soup = bs(r.text, \"html.parser\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "scrolled": false, + "tags": [] + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['table', 'table-striped', 'table-bordered', 'table-hover']\n['table', 'table-striped', 'table-bordered', 'table-hover']\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[[['S.NO.',\n", + " 'OFFICE/STATE',\n", + " 'FROM',\n", + " 'TO',\n", + " 'LOKSABHA SEAT',\n", + " 'ASSEMBLY SEAT',\n", + " 'RAJYA SABHA SEAT'],\n", + " ['President', '25.07.2017', '24.07.2022'],\n", + " ['Vice-President', '11.08.2017', '10.08.2022'],\n", + " ['RAJYA SABHA',\n", + " 'Permanent House. Members\\n\\t\\t\\t\\tare elected for a term\\n\\t\\t\\t\\tof six Years. Appx.\\n\\t\\t\\t\\t1/3 rd Members retire\\n\\t\\t\\t\\tbiennially',\n", + " '233 + 12'],\n", + " ['LOK SABHA', 'Term of House', '5 Years', '543+2'],\n", + " ['17.06.2019', '16.06.2024']]],\n", + " [[['S.NO.',\n", + " 'HOUSE/STATE',\n", + " 'FROM',\n", + " 'TO',\n", + " 'LOKSABHA SEAT',\n", + " 'ASSEMBLY SEAT',\n", + " 'RAJYA SABHA SEAT'],\n", + " ['1', 'ANDHRA PRADESH', '12.06.2019', '11.06.2024', '25', '175', '11'],\n", + " ['2', 'ARUNACHAL PRADESH', '03.06.2019', '02.06.2024', '2', '60', '1'],\n", + " ['3', 'ASSAM', '-', '-', '14', '126', '7'],\n", + " ['4', 'BIHAR', '-', '-', '40', '243', '16'],\n", + " ['5', 'CHHATISGARH', '04.01.2019', '03.01.2024', '11', '90', '5'],\n", + " ['6', 'GOA', '16.03.2017', '15.03.2022', '2', '40', '1'],\n", + " ['7', 'GUJARAT', '19.02.2018', '18.02.2023', '26', '182', '11'],\n", + " ['8', 'HARYANA', '04.11.2019', '03.11.2024', '10', '90', '5'],\n", + " ['9', 'HIMACHAL PRADESH', '09.01.2018', '08.01.2023', '4', '68', '3'],\n", + " ['10', 'JAMMU & KASHMIR*', '17.03.2015', '16.03.2021', '6', '87', '4'],\n", + " ['11', 'JHARKHAND', '06.01.2020', '05.01.2025', '14', '81', '6'],\n", + " ['12', 'KARNATAKA', '25.05.2018', '24.05.2023', '28', '224', '12'],\n", + " ['13', 'KERALA', '-', '-', '20', '140', '9'],\n", + " ['14', 'MADHYA PRADESH', '07.01.2019', '06.01.2024', '29', '230', '11'],\n", + " ['15', 'MAHARASHTRA', '27.11.2019', '26.11.2024', '48', '288', '19'],\n", + " ['16', 'MANIPUR', '20.03.2017', '19.03.2022', '2', '60', '1'],\n", + " ['17', 'MEGHALAYA', '16.03.2018', '15.03.2023', '2', '60', '1'],\n", + " ['18', 'MIZORAM', '18.12.2018', '17.12.2023', '1', '40', '1'],\n", + " ['19', 'NAGALAND', '13.03.2018', '12.03.2023', '1', '60', '1'],\n", + " ['20', 'NCT DELHI', '24.02.2020', '23.02.2025', '7', '70', '3'],\n", + " ['21', 'ODISHA', '25.06.2019', '24.06.2024', '21', '147', '10'],\n", + " ['22', 'PUDUCHERRY', '-', '-', '1', '30', '1'],\n", + " ['23', 'PUNJAB', '28.03.2017', '27.03.2022', '13', '117', '7'],\n", + " ['24', 'RAJASTHAN', '15.01.2019', '14.01.2024', '25', '200', '10'],\n", + " ['25', 'SIKKIM', '03.06.2019', '02.06.2024', '1', '32', '1'],\n", + " ['26', 'TAMIL NADU', '-', '-', '39', '234', '18'],\n", + " ['27', 'TELANGANA', '17.01.2019', '16.01.2024', '17', '119', '7'],\n", + " ['28', 'TRIPURA', '23.03.2018', '22.03.2023', '2', '60', '1'],\n", + " ['29', 'UTTAR PRADESH', '15.05.2017', '14.05.2022', '80', '403', '31'],\n", + " ['30', 'UTTARAKHAND', '24.03.2017', '23.03.2022', '5', '70', '3'],\n", + " ['31', 'WEST BENGAL', '-', '-', '42', '294', '16'],\n", + " ['One seat each for UTs of ANI, CH, DNH, DD and LKD', '5'],\n", + " ['Nominated Members in Rajya Sabha', '12'],\n", + " ['Nominated Members in Lok Sabha', '2'],\n", + " ['**545', '4120', '245***']]]]" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "all_data = []\n", + "tables = soup.find_all('table')\n", + "for table in tables:\n", + " print(table['class'])\n", + " tdata = []\n", + " table_bodies = table.find_all('tbody')\n", + "\n", + " for table_body in table_bodies:\n", + " data = []\n", + " rows = table_body.find_all('tr')\n", + " for row in rows:\n", + " cols = row.find_all('td')\n", + " if len(cols) == 0:\n", + " cols = row.find_all('th')\n", + " cols = [ele.text.strip() for ele in cols]\n", + " data.append([ele for ele in cols if ele]) # Get rid of empty values\n", + " tdata.append(data)\n", + " all_data.append(tdata)\n", + "all_data\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "string indices must be integers", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrow\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrow\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: string indices must be integers" + ] + } + ], + "source": [ + "tables = soup.find_all('table')\n", + "for table in tables:\n", + " data_dict = xmltodict.parse(table.prettify())\n", + " for val in data_dict.values():\n", + " for key in val.keys():\n", + " table_body = val['tbody']\n", + " for row in table_body:\n", + " if row == None:\n", + " continue\n", + " for key in row:\n", + " print(key, row[key])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['table', 'table-striped', 'table-hover']\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[[],\n", + " [{'HOUSE/STATE': 'GOA',\n", + " 'FROM': '16.03.2017',\n", + " 'TO': '15.03.2022',\n", + " 'SEAT': '40'},\n", + " {'HOUSE/STATE': 'MANIPUR',\n", + " 'FROM': '20.03.2017',\n", + " 'TO': '19.03.2022',\n", + " 'SEAT': '60'},\n", + " {'HOUSE/STATE': 'UTTARAKHAND',\n", + " 'FROM': '24.03.2017',\n", + " 'TO': '23.03.2022',\n", + " 'SEAT': '70'},\n", + " {'HOUSE/STATE': 'PUNJAB',\n", + " 'FROM': '28.03.2017',\n", + " 'TO': '27.03.2022',\n", + " 'SEAT': '117'},\n", + " {'HOUSE/STATE': 'UTTAR PRADESH',\n", + " 'FROM': '15.05.2017',\n", + " 'TO': '14.05.2022',\n", + " 'SEAT': '403'}]]]" + ] + }, + "metadata": {}, + "execution_count": 5 + } + ], + "source": [ + "all_data = []\n", + "tables = soup.find_all('table')\n", + "for table in tables:\n", + " print(table['class'])\n", + " tdata = []\n", + " table_bodies = table.find_all('tbody')\n", + "\n", + " for table_body in table_bodies:\n", + " data = []\n", + " keys = []\n", + " rows = table_body.find_all('tr')\n", + " for row in rows:\n", + " row_data = dict()\n", + " cols = row.find_all('td')\n", + " if len(cols) == 0:\n", + " cols = row.find_all('th')\n", + " keys = [ele.text.strip() for ele in cols]\n", + " continue\n", + " for i, ele in enumerate(cols):\n", + " row_data[keys[i]] = ele.text.strip()\n", + " data.append(row_data)\n", + " tdata.append(data)\n", + " all_data.append(tdata)\n", + "all_data\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": "[ S.NO. OFFICE/STATE FROM \\\n 0 NaN President 25.07.2017 \n 1 NaN Vice-President 11.08.2017 \n 2 NaN RAJYA SABHA Permanent House. Members are elected for a te... \n 3 NaN LOK SABHA Term of House \n 4 NaN LOK SABHA 17.06.2019 \n \n TO LOKSABHA SEAT \\\n 0 24.07.2022 NaN \n 1 10.08.2022 NaN \n 2 Permanent House. Members are elected for a te... NaN \n 3 5 Years 543+2 \n 4 16.06.2024 543+2 \n \n ASSEMBLY SEAT RAJYA SABHA SEAT \n 0 NaN NaN \n 1 NaN NaN \n 2 NaN 233 + 12 \n 3 NaN NaN \n 4 NaN NaN ,\n S.NO. HOUSE/STATE \\\n 0 1.0 ANDHRA PRADESH \n 1 2.0 ARUNACHAL PRADESH \n 2 3.0 ASSAM \n 3 4.0 BIHAR \n 4 5.0 CHHATISGARH \n 5 6.0 GOA \n 6 7.0 GUJARAT \n 7 8.0 HARYANA \n 8 9.0 HIMACHAL PRADESH \n 9 10.0 JAMMU & KASHMIR* \n 10 11.0 JHARKHAND \n 11 12.0 KARNATAKA \n 12 13.0 KERALA \n 13 14.0 MADHYA PRADESH \n 14 15.0 MAHARASHTRA \n 15 16.0 MANIPUR \n 16 17.0 MEGHALAYA \n 17 18.0 MIZORAM \n 18 19.0 NAGALAND \n 19 20.0 NCT DELHI \n 20 21.0 ODISHA \n 21 22.0 PUDUCHERRY \n 22 23.0 PUNJAB \n 23 24.0 RAJASTHAN \n 24 25.0 SIKKIM \n 25 26.0 TAMIL NADU \n 26 27.0 TELANGANA \n 27 28.0 TRIPURA \n 28 29.0 UTTAR PRADESH \n 29 30.0 UTTARAKHAND \n 30 31.0 WEST BENGAL \n 31 NaN One seat each for UTs of ANI, CH, DNH, DD and LKD \n 32 NaN Nominated Members in Rajya Sabha \n 33 NaN Nominated Members in Lok Sabha \n 34 NaN NaN \n \n FROM \\\n 0 12.06.2019 \n 1 03.06.2019 \n 2 - \n 3 - \n 4 04.01.2019 \n 5 16.03.2017 \n 6 19.02.2018 \n 7 04.11.2019 \n 8 09.01.2018 \n 9 17.03.2015 \n 10 06.01.2020 \n 11 25.05.2018 \n 12 - \n 13 07.01.2019 \n 14 27.11.2019 \n 15 20.03.2017 \n 16 16.03.2018 \n 17 18.12.2018 \n 18 13.03.2018 \n 19 24.02.2020 \n 20 25.06.2019 \n 21 - \n 22 28.03.2017 \n 23 15.01.2019 \n 24 03.06.2019 \n 25 - \n 26 17.01.2019 \n 27 23.03.2018 \n 28 15.05.2017 \n 29 24.03.2017 \n 30 - \n 31 One seat each for UTs of ANI, CH, DNH, DD and LKD \n 32 Nominated Members in Rajya Sabha \n 33 Nominated Members in Lok Sabha \n 34 NaN \n \n TO LOKSABHA SEAT \\\n 0 11.06.2024 25 \n 1 02.06.2024 2 \n 2 - 14 \n 3 - 40 \n 4 03.01.2024 11 \n 5 15.03.2022 2 \n 6 18.02.2023 26 \n 7 03.11.2024 10 \n 8 08.01.2023 4 \n 9 16.03.2021 6 \n 10 05.01.2025 14 \n 11 24.05.2023 28 \n 12 - 20 \n 13 06.01.2024 29 \n 14 26.11.2024 48 \n 15 19.03.2022 2 \n 16 15.03.2023 2 \n 17 17.12.2023 1 \n 18 12.03.2023 1 \n 19 23.02.2025 7 \n 20 24.06.2024 21 \n 21 - 1 \n 22 27.03.2022 13 \n 23 14.01.2024 25 \n 24 02.06.2024 1 \n 25 - 39 \n 26 16.01.2024 17 \n 27 22.03.2023 2 \n 28 14.05.2022 80 \n 29 23.03.2022 5 \n 30 - 42 \n 31 One seat each for UTs of ANI, CH, DNH, DD and LKD 5 \n 32 Nominated Members in Rajya Sabha NaN \n 33 Nominated Members in Lok Sabha 2 \n 34 NaN **545 \n \n ASSEMBLY SEAT RAJYA SABHA SEAT \n 0 175.0 11 \n 1 60.0 1 \n 2 126.0 7 \n 3 243.0 16 \n 4 90.0 5 \n 5 40.0 1 \n 6 182.0 11 \n 7 90.0 5 \n 8 68.0 3 \n 9 87.0 4 \n 10 81.0 6 \n 11 224.0 12 \n 12 140.0 9 \n 13 230.0 11 \n 14 288.0 19 \n 15 60.0 1 \n 16 60.0 1 \n 17 40.0 1 \n 18 60.0 1 \n 19 70.0 3 \n 20 147.0 10 \n 21 30.0 1 \n 22 117.0 7 \n 23 200.0 10 \n 24 32.0 1 \n 25 234.0 18 \n 26 119.0 7 \n 27 60.0 1 \n 28 403.0 31 \n 29 70.0 3 \n 30 294.0 16 \n 31 NaN NaN \n 32 NaN 12 \n 33 NaN NaN \n 34 4120.0 245*** ]" + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[{'S.NO.': nan, 'OFFICE/STATE': 'President', 'FROM': '25.07.2017', 'TO': '24.07.2022', 'LOKSABHA SEAT': nan, 'ASSEMBLY SEAT': nan, 'RAJYA SABHA SEAT': nan}, {'S.NO.': nan, 'OFFICE/STATE': 'Vice-President', 'FROM': '11.08.2017', 'TO': '10.08.2022', 'LOKSABHA SEAT': nan, 'ASSEMBLY SEAT': nan, 'RAJYA SABHA SEAT': nan}, {'S.NO.': nan, 'OFFICE/STATE': 'RAJYA SABHA', 'FROM': 'Permanent House. Members are elected for a term of six Years. Appx. 1/3 rd Members retire biennially', 'TO': 'Permanent House. Members are elected for a term of six Years. Appx. 1/3 rd Members retire biennially', 'LOKSABHA SEAT': nan, 'ASSEMBLY SEAT': nan, 'RAJYA SABHA SEAT': '233 + 12'}, {'S.NO.': nan, 'OFFICE/STATE': 'LOK SABHA', 'FROM': 'Term of House', 'TO': '5 Years', 'LOKSABHA SEAT': '543+2', 'ASSEMBLY SEAT': nan, 'RAJYA SABHA SEAT': nan}, {'S.NO.': nan, 'OFFICE/STATE': 'LOK SABHA', 'FROM': '17.06.2019', 'TO': '16.06.2024', 'LOKSABHA SEAT': '543+2', 'ASSEMBLY SEAT': nan, 'RAJYA SABHA SEAT': nan}]\n" + ] + } + ], + "source": [ + "all_data = []\n", + "tables = pd.read_html(r.text)\n", + "display(tables)\n", + "print(tables[0].to_dict(orient='records'))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "union = tables[0]\n", + "state = tables[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": "0 False\n1 False\n2 False\n3 True\n4 True\nName: OFFICE/STATE, dtype: bool" + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": "0 True\n1 True\n2 False\n3 False\n4 True\nName: TO, dtype: bool" + }, + "metadata": {} + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'16.06.2024'" + ] + }, + "metadata": {}, + "execution_count": 44 + } + ], + "source": [ + "# end_dates = list(union[union['OFFICE/STATE'] == \"LOK SABHA\"]['TO'])\n", + "# [d for d in end_dates if len(d.split('.')) == 3]\n", + "\n", + "# union[union['OFFICE/STATE'] == \"LOK SABHA\"].filter(regex='^\\d+\\.\\d+\\.\\d+$', axis=1)\n", + "lok_sabha = union[(union['OFFICE/STATE'] == \"LOK SABHA\") & (union['TO'].str.contains('^\\d+\\.\\d+\\.\\d+$'))]\n", + "lok_sabha['TO'].values[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['26.11.2024']" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ], + "source": [ + "end_dates = list(state[state['HOUSE/STATE'] == 'MAHARASHTRA']['TO'])\n", + "[d for d in end_dates if len(str(d).split('.')) == 3]\n", + "# end_dates" + ] + } + ], + "metadata": { + "kernelspec": { + "name": "python385jvsc74a57bd031f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6", + "display_name": "Python 3.8.5 64-bit" + }, + "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.8.5" + }, + "metadata": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file