Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Q 4 . Please create two bar plots as per below that show: 1 ) The number of individuals who have a High School Graduate

Q4. Please create two bar plots as per below that show:
1) The number of individuals who have a High School Graduate Diploma AND earn <=50K in the United States
2) The number of individuals who have a High School Graduate Diploma AND earn >50K in the United States
Working on a python question and they are asking to create a separate graph for each of these.
Code :
hs_graduates_low_income = df[(df['Degree Status']== 'High School Grad') & (df['Earnings']<=50000) & (df['Country']== 'United States')]
hs_graduates_high_income = df[(df['Degree Status']== 'High School Grad') & (df['Earnings']>50000) & (df['Country']== 'United States')]
# Step 4: Count the number of individuals in each category.
low_income_counts = hs_graduates_low_income.shape[0]
high_income_counts = hs_graduates_high_income.shape[0]
# Step 5: Create bar plots.
# Bar plot for individuals with High School Graduate Diploma and <=50K income
plt.bar(['<=50K'],[low_income_counts], color='blue', label='<=50K')
# Bar plot for individuals with High School Graduate Diploma and >50K income
plt.bar(['>50K'],[high_income_counts], color='orange', label='>50K')
# Add labels and title
plt.xlabel('Earnings')
plt.ylabel('Number of Individuals')
plt.title('Number of High School Graduates by Income in the United States')
# Add legend
plt.legend()
# Show the plot
plt.show()
error
KeyError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3802, in Index.get_loc(self, key, method, tolerance)
3801 try:
->3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
File ~\anaconda3\lib\site-packages\pandas\_libs\index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()
File ~\anaconda3\lib\site-packages\pandas\_libs\index.pyx:165, in pandas._libs.index.IndexEngine.get_loc()
File pandas\_libs\hashtable_class_helper.pxi:5745, in pandas._libs.hashtable.PyObjectHashTable.get_item()
File pandas\_libs\hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Degree Status'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Cell In[48], line 1
---->1 hs_graduates_low_income = df[(df['Degree Status']== 'High School Grad') & (df['Earnings']<=50000) & (df['Country']== 'United States')]
2 hs_graduates_high_income = df[(df['Degree Status']== 'High School Grad') & (df['Earnings']>50000) & (df['Country']== 'United States')]
4 # Step 4: Count the number of individuals in each category.
File ~\anaconda3\lib\site-packages\pandas\core\frame.py:3807, in DataFrame.__getitem__(self, key)
3805 if self.columns.nlevels >1:
3806 return self._getitem_multilevel(key)
->3807 indexer = self.columns.get_loc(key)
3808 if is_integer(indexer):
3809 indexer =[indexer]
File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3804, in Index.get_loc(self, key, method, tolerance)
3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
->3804 raise KeyError(key) from err
3805 except TypeError:
3806 # If we have a listlike key, _check_indexing_error will raise
3807 # InvalidIndexError. Otherwise we fall through and re-raise
3808 # the TypeError.
3809 self._check_indexing_error(key)
KeyError: 'Degree Status'

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students explore these related Databases questions