import json
=json.load(open('/home/stephy/Documents/class/Python Website/Choropleth/KeralaLAC_QID.geojson')) KeralaLAC
Kerala LAC 2021 results
The geojson file describing the boundaries of the Kerala Assembly constituencies are avaialble in the Kaggle. The data may be downloaded from Kaggle using the link.
Now we shall see the data relating to each constituency as follows
'features'][0] KeralaLAC[
{'type': 'Feature',
'properties': {'State': 'KERALA',
'District': 'KASARAGOD',
'Asmbly_Con': 'Manjeshwaram',
'Prlmnt_Con': 'KASARAGOD',
'Area(sqkm)': 378.6475933415851,
'WikiData': 'Q13113782'},
'geometry': {'type': 'MultiPolygon',
'coordinates': [[[[75.1477492255188, 12.634487036338157],
[75.14545963673042, 12.634487036338157],
[75.14673163050175, 12.626830779509708],
[75.15563558690093, 12.620075258778723],
[75.15410919437535, 12.60611384926802],
[75.14596843423895, 12.602961272926894],
[75.1327396990173, 12.603411640975628],
[75.1273973251778, 12.616021946340133],
[75.11111580490501, 12.615121210242668],
[75.11137020365928, 12.621876730973653],
[75.10526463355697, 12.629082619753369],
[75.0821143469191, 12.629532987802103],
[75.05947285778974, 12.618724154632528],
[75.05692887024712, 12.626380411460975],
[75.05336728768745, 12.626380411460975],
[75.04675292007663, 12.622777467071117],
[75.04802491384794, 12.617373050486329],
[75.04141054623712, 12.60971679365788],
[75.0279274122612, 12.614220474145203],
[75.01368108202252, 12.60386200902436],
[74.98442522528235, 12.561527412443521],
[74.97552126888316, 12.570985141466899],
[74.96000294487315, 12.566481460979576],
[74.95389737477086, 12.558374836102395],
[74.95466057103364, 12.549367475127747],
[74.93863344951512, 12.591251703659854],
[74.94194063332053, 12.58674802317253],
[74.9401598420407, 12.595305016098445],
[74.94779180466857, 12.60611384926802],
[74.95415177352513, 12.605213113170556],
[74.94194063332053, 12.607464953414219],
[74.93965104453217, 12.595755384147179],
[74.9320190819043, 12.614670842193934],
[74.94168623456626, 12.632235196094497],
[74.95008139345693, 12.636288508533088],
[74.94244943082906, 12.637189244630552],
[74.92820310059037, 12.61196863390154],
[74.8880080974169, 12.70789702828153],
[74.8969120538161, 12.697088195111952],
[74.90632480772379, 12.695286722917022],
[74.89894724385019, 12.69843929925815],
[74.89360487001068, 12.711499972671387],
[74.89156967997658, 12.71240070876885],
[74.89843844634166, 12.714202180963781],
[74.88673610364559, 12.71240070876885],
[74.88495531236576, 12.710599236573922],
[74.88699050239985, 12.708797764378993],
[74.8847009136115, 12.71014886852519],
[74.86765619707592, 12.751582729008565],
[74.86969138711001, 12.761040458031943],
[74.87427056468674, 12.755185673398422],
[74.88241132482314, 12.754284937300959],
[74.93303667692135, 12.780856652176166],
[74.95847655234759, 12.784909964614757],
[74.96025734362742, 12.779055179981237],
[74.96458212244988, 12.781757388273629],
[74.96865250251808, 12.776803339737574],
[74.98747801033349, 12.783108492419828],
[74.99231158666448, 12.788512909004616],
[74.98925880161333, 12.795718797784332],
[74.99841715676678, 12.786261068760954],
[75.00274193558924, 12.788512909004616],
[75.00146994181793, 12.792115853394474],
[75.00554032188613, 12.790764749248277],
[75.00401392936055, 12.777253707786308],
[75.00986510070858, 12.767795978762928],
[74.98747801033349, 12.742124999985185],
[74.98417082652809, 12.744376840228846],
[74.98391642777382, 12.736720583400398],
[74.98620601656218, 12.734018375108004],
[74.99231158666448, 12.739873159741524],
[75.013172284514, 12.72771322242575],
[75.0009611443094, 12.716454021207444],
[75.01495307579383, 12.714202180963781],
[75.01851465835351, 12.720957701694765],
[75.03912095744876, 12.72411027803589],
[75.0523496926704, 12.720056965597301],
[75.05209529391614, 12.713301444866316],
[75.05413048395023, 12.706996292184062],
[75.04980570512778, 12.703843715842936],
[75.05514807896729, 12.692134146575896],
[75.04370013502547, 12.692584514624627],
[75.04039295122007, 12.68492825779618],
[75.04649852132236, 12.667814271944351],
[75.05413048395023, 12.670066112188012],
[75.05769206650992, 12.665562431700689],
[75.05921845903548, 12.671867584382941],
[75.06863121294319, 12.664661695603225],
[75.0739735867827, 12.672317952431673],
[75.08593032823303, 12.6754705287728],
[75.08516713197025, 12.695737090965755],
[75.08084235314779, 12.698889667306881],
[75.08593032823303, 12.703843715842936],
[75.10628222857402, 12.696187459014487],
[75.11111580490501, 12.681775681455052],
[75.12205495133828, 12.67907347316266],
[75.12485333763517, 12.681775681455052],
[75.13248530026304, 12.675920896821532],
[75.15156520683273, 12.68312678560125],
[75.14953001679862, 12.673669056577872],
[75.16174115700322, 12.671867584382941],
[75.15054761181568, 12.649799549995057],
[75.13808207285682, 12.643044029264072],
[75.1477492255188, 12.634487036338157]]]]}}
These are the vertices of the polygon corresponding to the first assembly constituency, Manjeshwaram. Similarly we can find the data relating to other constituencies from the same data set by changing the indices.
'features'][10]['properties'] KeralaLAC[
{'State': 'KERALA',
'District': 'KANNUR',
'Asmbly_Con': 'Mattannur',
'Prlmnt_Con': 'KANNUR',
'Area(sqkm)': 346.1118696878761,
'WikiData': 'Q16134425'}
We have the data corresponding to the assembly constituency Mattannur using the index 10.
Now we shall try to import the data corresponing to Kerala Assembly Elections 2021 avaliable in kaggle.
import pandas as pd
=pd.read_csv('/home/stephy/Documents/class/Python Website/Choropleth/Kerala_election_2021.csv')
election election.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 140 entries, 0 to 139
Data columns (total 18 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 CONSTITUENCY 140 non-null object
1 DISTRICT 140 non-null object
2 LDF 140 non-null int64
3 UDF 140 non-null int64
4 NDA 140 non-null int64
5 NOTA 140 non-null int64
6 OTHERS 140 non-null int64
7 EVM VOTES 140 non-null int64
8 POSTAL VOTES 140 non-null int64
9 TOTAL VOTES 140 non-null int64
10 LEAD 140 non-null int64
11 WINNER 140 non-null object
12 %LDF 140 non-null float64
13 %UDF 140 non-null float64
14 %NDA 140 non-null float64
15 %NOTA 140 non-null float64
16 %POSTAL 140 non-null float64
17 %LEAD 140 non-null float64
dtypes: float64(6), int64(9), object(3)
memory usage: 19.8+ KB
We can see that the data is sorted in the alphabetical order in the results file and the data is presented in another order in geojson file. Let us verify whether the names of the constituencies, which will be used to link the data with the GeoJSON file, are spelled the same in both files.
=[]
mismatch
for i in range(140):
=KeralaLAC['features'][i]['properties']['Asmbly_Con']
consNameif (consName not in list(election['CONSTITUENCY'])):
mismatch.append(consName)print(mismatch)
['Kalliasseri', 'Mananthavady (ST)', 'Sulthanbathery (S', 'Kuttiadi', 'Balusseri (SC)', 'Ernad', 'Wandoor (SC)', 'Mannarkkad', 'Vallikunnu', 'Kongad(SC)', 'Malampuzha', 'Thrithala', 'Chelakkara (SC)', 'Tarur (SC)', 'Wadakkanchery', 'Nemmara', 'Nattika(SC)', 'Chalakudy', 'Irinjalakuda', 'Devikulam (SC)', 'Vypeen', 'Kunnathunad (SC)', 'Thrikkakara', 'Thripunithura', 'Piravom', 'Vaikom (SC)', 'Peerumade', 'Puthuppally', 'Kanjirappally', 'Kuttanad', 'Changanassery', 'Ambalappuzha', 'Chengannur', 'Mavelikkara (SC)', 'Adoor (SC)', 'Karunagappally', 'Kunnathur (SC)', 'Chathannoor', 'Attingal (SC)', 'Chirayinkeezhu', 'Kazhakoottam', 'Vattiyoorkavu(SC)', 'Thiruvananthapura']
We will update the data in the respective data file and the new data will be imported in Python.
=pd.read_csv('/home/stephy/Documents/class/Python Website/Choropleth/Kerala_election_2021_Revised.csv')
election_revised election_revised.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 140 entries, 0 to 139
Data columns (total 18 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 CONSTITUENCY 140 non-null object
1 DISTRICT 140 non-null object
2 LDF 140 non-null int64
3 UDF 140 non-null int64
4 NDA 140 non-null int64
5 NOTA 140 non-null int64
6 OTHERS 140 non-null int64
7 EVM VOTES 140 non-null int64
8 POSTAL VOTES 140 non-null int64
9 TOTAL VOTES 140 non-null int64
10 LEAD 140 non-null int64
11 WINNER 140 non-null object
12 %LDF 140 non-null float64
13 %UDF 140 non-null float64
14 %NDA 140 non-null float64
15 %NOTA 140 non-null float64
16 %POSTAL 140 non-null float64
17 %LEAD 140 non-null float64
dtypes: float64(6), int64(9), object(3)
memory usage: 19.8+ KB
Now let us have a look at how this file look like.
election_revised.head()
CONSTITUENCY | DISTRICT | LDF | UDF | NDA | NOTA | OTHERS | EVM VOTES | POSTAL VOTES | TOTAL VOTES | LEAD | WINNER | %LDF | %UDF | %NDA | %NOTA | %POSTAL | %LEAD | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Alappuzha | Alappuzha | 73412 | 61768 | 21650 | 1089 | 527 | 154149 | 4297 | 158446 | 11644 | LDF | 46.332504 | 38.983628 | 13.663961 | 0.687300 | 2.711965 | 7.348876 |
1 | Ambalappuzha | Alappuzha | 61365 | 50240 | 22389 | 591 | 2436 | 133382 | 3639 | 137021 | 11125 | LDF | 44.785106 | 36.665913 | 16.339831 | 0.431321 | 2.655797 | 8.119193 |
2 | Aroor | Alappuzha | 75617 | 68604 | 17479 | 846 | 1945 | 160851 | 3640 | 164491 | 7013 | LDF | 45.970296 | 41.706841 | 10.626113 | 0.514314 | 2.212887 | 4.263455 |
3 | Chengannur | Alappuzha | 71502 | 39409 | 34620 | 491 | 1149 | 142958 | 4213 | 147171 | 32093 | LDF | 48.584300 | 26.777694 | 23.523656 | 0.333626 | 2.862656 | 21.806606 |
4 | Cherthala | Alappuzha | 83702 | 77554 | 14562 | 602 | 1677 | 172214 | 5883 | 178097 | 6148 | LDF | 46.997984 | 43.545933 | 8.176443 | 0.338018 | 3.303256 | 3.452051 |
import plotly.express as px
= px.choropleth(
fig
election_revised,=KeralaLAC,
geojson='CONSTITUENCY', # Column in your DataFrame
locations='WINNER', # Column for coloring
color='properties.Asmbly_Con' # Key in GeoJSON that matches `locations`
featureidkey
)='locations',visible=False)
fig.update_geos(fitbounds
fig.to_html() fig.show()
Now we shall try to show the constituencies with coalition that won in the constituency.
import plotly.express as px
= px.choropleth(
fig
election_revised,=KeralaLAC,
geojson='CONSTITUENCY', # Column in your DataFrame
locations='WINNER', # Column for coloring
color='properties.Asmbly_Con' # Key in GeoJSON that matches `locations`
featureidkey
)='locations',visible=False)
fig.update_geos(fitbounds
fig.to_html() fig.show()
Now we shall assign specific colours to each coalition- UDF -Blue and LDF -Red.
import plotly.express as px
={'UDF':'blue',
coal_colour'LDF':'red'}
= px.choropleth(
fig
election_revised,=KeralaLAC,
geojson='CONSTITUENCY', # Column in your DataFrame
locations='WINNER', # Column for coloring
color='properties.Asmbly_Con', # Key in GeoJSON that matches `locations`
featureidkey=coal_colour
color_discrete_map
)='locations',visible=False)
fig.update_geos(fitbounds
fig.to_html() fig.show()
Now we shall use the intensity of colours to show the lead each coalition got in the constituency, again sticking on to the colour pattern.
import plotly.express as px
import json
import pandas as pd
# Define a Numeric Mapping for Coalition
= {"UDF": 1, "LDF": -1} # UDF → 1 (Blue), LDF → -1 (Red)
coalition_map "COALITION_NUM"] = election_revised["WINNER"].map(coalition_map) * election_revised["%LEAD"]
election_revised[
# Create Choropleth
= px.choropleth(
fig
election_revised,=KeralaLAC,
geojson="CONSTITUENCY", # Matches with GeoJSON
locations="properties.Asmbly_Con", # Ensure this matches GeoJSON structure
featureidkey="COALITION_NUM", # Numeric color mapping
color='RdBu', # Custom Red-White-Blue Scale
color_continuous_scale="CONSTITUENCY",
hover_name=["WINNER", "%LEAD"]
hover_data
)
# Update Layout
="locations", visible=False)
fig.update_geos(fitbounds="Election Results: Color by Coalition, Intensity by Lead %")
fig.update_layout(title
# Show Figure
fig.to_html() fig.show()