Python Forum
mapping does not work anymore
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mapping does not work anymore
#1
Hi, im trying to understand what is causing me my issue.It used to work!
Last time i ran this code was about a year ago
Is this python , pandas or streamlit ?
I have this code to map zips ( first 3 digits of the zips )
def area_zips(zipcode):
    zips = {"010": "Springfield", "011": "Springfield", "012": "Springfield", "013": "Springfield", "014": "Boston"}
    return zips[zipcode[:3]]
df["PuMarket"] = df["PuZip"].map(area_zips)
 
This code to map states:
    
def regions(region):
    states = {"ME": "Northeast", "NH": "Northeast"}
    return states[region]
df["PuRegion"] = df["PuState"].map(regions)
i get errors. Error sample for zip codes, but same errors if i remove zips and leave region code.
Error:
KeyError: '' Traceback: File "/development/koala/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling result = func() ^^^^^^ File "/development/koala/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 590, in code_to_exec exec(code, module.__dict__) File "/development/koala/1_Home.py", line 169, in <module> df = load_data() ^^^^^^^^^^^ File "/development/koala/lib/python3.12/site-packages/streamlit/runtime/caching/cache_utils.py", line 210, in __call__ return self._get_or_create_cached_value(args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/development/koala/lib/python3.12/site-packages/streamlit/runtime/caching/cache_utils.py", line 235, in _get_or_create_cached_value return self._handle_cache_miss(cache, value_key, func_args, func_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/development/koala/lib/python3.12/site-packages/streamlit/runtime/caching/cache_utils.py", line 288, in _handle_cache_miss computed_value = self._info.func(*func_args, **func_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/development/koala/1_Home.py", line 102, in load_data df["PuMarket"] = df["PuZip"].map(area_zips) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/development/koala/lib/python3.12/site-packages/pandas/core/series.py", line 4700, in map new_values = self._map_values(arg, na_action=na_action) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/development/koala/lib/python3.12/site-packages/pandas/core/base.py", line 921, in _map_values return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/development/koala/lib/python3.12/site-packages/pandas/core/algorithms.py", line 1743, in map_array return lib.map_infer(values, mapper, convert=convert) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib.pyx", line 2972, in pandas._libs.lib.map_infer File "/development/koala/1_Home.py", line 100, in area_zips return zips[zipcode[:3]] ~~~~^^^^^^^^^^^^^
Thank You.
Reply
#2
Somewhere in your dataframe you have " " in the PuZip column.

This works:
import pandas as pd

def area_zips(zipcode):
    zips = {"010": "Springfield", "011": "Springfield", "012": "Springfield", "013": "Springfield", "014": "Boston"}
    return zips[zipcode[:3]]

df = pd.DataFrame({"PuZip": ["01000", "01100", "01200", "01300", "01400"]})
df["PuMarket"] = df["PuZip"].map(area_zips)
print(df)
Output:
PuZip PuMarket 0 01000 Springfield 1 01100 Springfield 2 01200 Springfield 3 01300 Springfield 4 01400 Boston
But if I add " " to PuZip:
df = pd.DataFrame({"PuZip": ["01000", "01100", "01200", "01300", "01400", " "]})
I get the same error you have.
Error:
File "test.py", line 5, in area_zips return zips[zipcode[:3]] ~~~~^^^^^^^^^^^^^ KeyError: ' '
An easy way to find the bad line, and something you should always do, is provide a default value for when the map function fails.
def area_zips(zipcode):
    zips = {"010": "Springfield", "011": "Springfield", "012": "Springfield", "013": "Springfield", "014": "Boston"}
    return zips.get(zipcode[:3])
Now instead of a crash, I get a dataframe that has None
Output:
PuZip PuMarket 0 01000 Springfield 1 01100 Springfield 2 01200 Springfield 3 01300 Springfield 4 01400 Boston 5 None
If you don't like None, you can provide any default value you want for zips.get().
Reply
#3
It is hard to spot the issues. Thanks for sharing and updating the important information.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020