Question: Question below is the python code for tree map. I have connected to sql database. However, when I am trying to execute the below code
- Question below is the python code for tree map. I have connected to sql database. However, when I am trying to execute the below code on jupyterhub, it throws up key error. Whereas, the variables column name in sql table in the sql database is exactly as it is mentioned below?! Advise please.
import pandas as pd
import MySQLdb
import matplotlib.pyplot as plt
import squarify
df=pd.read_sql(\"SELECT * from INTROPROJECT\",conn)
df
data_df = pd.DataFrame(df)
data_df
f = plt.figure()
f.set_figwidth(25)
f.set_figheight(12)
squarify.plot(sizes=df['age','bp','sg','al','su','rbc','pc','pcc','ba','bgr','bu','sc','sod','pot','hemo','pcv','wc','rc','htn','dm','cad','appet','pe','ane'], label=df['classification'],alpha=1)
plt.axis('off')
plt.show()
error -
KeyError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2894try:
-> 2895 return self._engine.get_loc(casted_key)
2896except KeyError as err:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('age', 'bp', 'sg', 'al', 'su', 'rbc', 'pc', 'pcc', 'ba', 'bgr', 'bu', 'sc', 'sod', 'pot', 'hemo', 'pcv', 'wc', 'rc', 'htn', 'dm', 'cad', 'appet', 'pe', 'ane')
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
in
10f.set_figwidth(25)
11f.set_figheight(12)
---> 12 squarify.plot(sizes=df['age','bp','sg','al','su','rbc','pc','pcc','ba','bgr','bu','sc','sod','pot','hemo','pcv','wc','rc','htn','dm','cad','appet','pe','ane'], label=df['classification'],alpha=1)
13plt.axis('off')
14plt.show()
/usr/local/lib/python3.8/dist-packages/pandas/core/frame.py in __getitem__(self, key)
2904if self.columns.nlevels > 1:
2905return self._getitem_multilevel(key)
-> 2906 indexer = self.columns.get_loc(key)
2907if is_integer(indexer):
2908indexer = [indexer]
/usr/local/lib/python3.8/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2895return self._engine.get_loc(casted_key)
2896except KeyError as err:
-> 2897 raise KeyError(key) from err
2898
2899if tolerance is not None:
KeyError: ('age', 'bp', 'sg', 'al', 'su', 'rbc', 'pc', 'pcc', 'ba', 'bgr', 'bu', 'sc', 'sod', 'pot', 'hemo', 'pcv', 'wc', 'rc', 'htn', 'dm', 'cad', 'appet', 'pe', 'ane')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
