In [50]: df_bank = pd.read_csv ("bank.csv") df_bank.head(10) # do not change this out [50]: age 0 59 1 56 2 41 3 55 4 54 5 42 management single 6 56 management married tertiary 7 8 9 60 37 28 job marital education default balance housing loan contact day month duration campaign pdays previous poutcome deposit admin. married secondary no 2343 yes no unknown 5 unknown unknown technician unknown admin. married secondary married secondary married secondary tertiary no tertiary 45 no 1270 2476 184 services unknown unknown admin. married 0 unknown unknown unknown unknown unknown In [58]: df = df_bank.head(10) married = df['marital'] no retired divorced secondary no technician married secondary no services single secondary no Question 3 ¶ 1. What is the average age for married people in this dataset? --> 958 no average_age_married = married ['age'].mean() print(avg_age_married) # do not change this no 962 963 df['marital'] KeyError Input In [58], in () 1 df df_bank.head(10) 3 married ----> 5 average_age_married = married ['age'].mean() 7 print (avg_age_married) 960 if is hashable(key): 961 830 545 1 5090 return self._get_value(key) . no no unknown yes πιο unknown yes no unknown по no unknown yes yes unknown yes unknown yes yes no unknown yes no unknown yes no unknown 5 5 5 5 5 6 self._check_indexing_error(key) 20 6 6 20 030 6 Traceback (most recent call last) may may may may may may may may may may File ~\anaconda3\lib\site-packages\pandas\core\series.py:958, in Series._getitem_(self, key) 955 return self._values [key] 957 elif key_is_scalar: 1042 1467 1389 579 673 562 1201 1030 608 1297 # otherwise index.get_value will raise InvalidIndexError try: # For labels that don't resolve as scalars like tuples and frozensets --> 389 raise KeyError(key) 390 return super ().get_loc (key, method-method, tolerance-tolerance) KeyError: 'age' 1 1 1 1 2 2 1 1 1 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 File ~\anaconda3\lib\site-packages\pandas\core\series.py: 1069, in series. _get_value(self, label, takeable) 1066 return self._values[label] 1868 # Similar to Index.get_value, but we do not fall back to positional -> 1069 loc= self.index.get_loc(label) 1878 return self.index._get_values_for_loc(self, loc, label) File ~\anaconda3\lib\site-packages\pandas\core\indexes \range.py: 389, in RangeIndex.get_loc(self, key, method, tolerance) 387 raise KeyError (key) from err 388 0 0 0 0 0 0 0 0 0 0 yes yes yes yes yes yes yes yes yes yes

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

Why am I getting this error? How can I fix it? please answer this properly

In [50]: df_bank = pd.read_csv ("bank.csv")
df_bank.head(10) # do not change this
out [50]:
age
0
59
1
56
2
41
3
55
4 54
5 42 management single
6
58 management married
60
37
28
8
9
job marital education default balance housing loan contact day month duration campaign pdays previous poutcome deposit
admin. married secondary no
1042
-1
0
unknown
2343
45
admin. married secondary
yes no unknown 5 may
no no unknown
5 may
5
may
1467
-1
0
unknown
married secondary
yes no unknown
1389
-1
0
unknown
unknown
married secondary
yes
no unknown
5
may
579
-1
0
married
no unknown
5
may
673
-1
0
unknown
yes yes unknown
5
562
0 unknown
may
may
yes yes unknown 6
0 unknown
no unknown 6 may
yes
yes
yes
unknown
unknown
no unknown
6
0
0
0
may
may
no unknown
6
unknown
technician
services
admin.
tertiary
tertiary
tertiary
retired divorced secondary
technician married secondary
services single secondary
In [58]: df = df_bank.head(10)
married = df['marital']
KeyError
Input In [58], in <cell line: 5>()
1 df = df_bank.head(10)
df['marital']
--> 958
no
957 elif key_is_scalar:
962
963
1270
2476
184
0
830
545
1
no 5090
Question 3 ¶
1. What is the average age for married people in this dataset?
no
2 2 2 2
average_age_married = married ['age'].mean()
print(avg_age_married) # do not change this
960 if is hashable(key):
961
no
no
no
3 married
----> 5 average_age_married = married ['age'].mean()
7 print (avg_age_married)
return self._get_value(key)
no
Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\pandas\core\series.py:958, in Series._getitem_(self, key)
955 return self._values [key]
1201
1030
608
1297
# otherwise index.get_value will raise InvalidIndexError
try:
# For labels that don't resolve as scalars like tuples and frozensets
1
1
1
1
2
2
1
-> 1069 loc = self.index.get_loc(label)
1070 return self.index._get_values_for_loc(self, loc, label)
1
1
3
-1
-1
-1
-1
-1
File ~\anaconda3\lib\site-packages\pandas\core\series.py: 1069, in Series._get_value(self, label, takeable)
1066
return self._values[label]
1068 # Similar to Index.get_value, but we do not fall back to positional
File ~\anaconda3\lib\site-packages\pandas\core\indexes \range.py: 389, in RangeIndex.get_loc(self, key, method, tolerance)
387
raise KeyError(key) from err
388
self._check_indexing_error(key)
--> 389
raise KeyError(key)
390 return super ().get_loc (key, method-method, tolerance-tolerance)
KeyError: 'age'
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
Transcribed Image Text:In [50]: df_bank = pd.read_csv ("bank.csv") df_bank.head(10) # do not change this out [50]: age 0 59 1 56 2 41 3 55 4 54 5 42 management single 6 58 management married 60 37 28 8 9 job marital education default balance housing loan contact day month duration campaign pdays previous poutcome deposit admin. married secondary no 1042 -1 0 unknown 2343 45 admin. married secondary yes no unknown 5 may no no unknown 5 may 5 may 1467 -1 0 unknown married secondary yes no unknown 1389 -1 0 unknown unknown married secondary yes no unknown 5 may 579 -1 0 married no unknown 5 may 673 -1 0 unknown yes yes unknown 5 562 0 unknown may may yes yes unknown 6 0 unknown no unknown 6 may yes yes yes unknown unknown no unknown 6 0 0 0 may may no unknown 6 unknown technician services admin. tertiary tertiary tertiary retired divorced secondary technician married secondary services single secondary In [58]: df = df_bank.head(10) married = df['marital'] KeyError Input In [58], in <cell line: 5>() 1 df = df_bank.head(10) df['marital'] --> 958 no 957 elif key_is_scalar: 962 963 1270 2476 184 0 830 545 1 no 5090 Question 3 ¶ 1. What is the average age for married people in this dataset? no 2 2 2 2 average_age_married = married ['age'].mean() print(avg_age_married) # do not change this 960 if is hashable(key): 961 no no no 3 married ----> 5 average_age_married = married ['age'].mean() 7 print (avg_age_married) return self._get_value(key) no Traceback (most recent call last) File ~\anaconda3\lib\site-packages\pandas\core\series.py:958, in Series._getitem_(self, key) 955 return self._values [key] 1201 1030 608 1297 # otherwise index.get_value will raise InvalidIndexError try: # For labels that don't resolve as scalars like tuples and frozensets 1 1 1 1 2 2 1 -> 1069 loc = self.index.get_loc(label) 1070 return self.index._get_values_for_loc(self, loc, label) 1 1 3 -1 -1 -1 -1 -1 File ~\anaconda3\lib\site-packages\pandas\core\series.py: 1069, in Series._get_value(self, label, takeable) 1066 return self._values[label] 1068 # Similar to Index.get_value, but we do not fall back to positional File ~\anaconda3\lib\site-packages\pandas\core\indexes \range.py: 389, in RangeIndex.get_loc(self, key, method, tolerance) 387 raise KeyError(key) from err 388 self._check_indexing_error(key) --> 389 raise KeyError(key) 390 return super ().get_loc (key, method-method, tolerance-tolerance) KeyError: 'age' yes yes yes yes yes yes yes yes yes yes
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Query Syntax
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education