简体   繁体   中英

I'm not sure why I am getting a KeyError: in the sample code below

The CSV file I am importing has a column named 'MED EXP DATE'. I reference this same column earlier in the code with no problem. When I reference it later, I get a key error.

import pandas as pd
df = pd.read_csv(r"C:\Users\Desktop\Air_Rally_Marketing\PILOT_BASIC.csv", low_memory=False, dtype={'UNIQUE ID': str, 'FIRST NAME': str,'LAST NAME': str, 'STREET 1': str, 'STREET 2': str, 'CITY': str, 'STATE': str, 'ZIP CODE': str, 'MED DATE': str, 'MED EXP DATE': str})
df.dropna(inplace = True)
df['EXP DATE LEN'] = df['MED EXP DATE'].apply(len) #creates a column to store the length of the column MED EXP DATE
print(df.head)

This is the error I receive:

return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 90, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 135, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index_class_helper.pxi", line 109, in pandas._libs.index.Int64Engine._check_type
KeyError: 'MED EXP DATE'

When I search the meaning of this error, my understanding is that it means I am referencing a key that cannot be found. I'm confused by this because I reference "MED EXP DATE" in a prior line and do not get the key error there.

This below:

df = Right = df['MED EXP DATE'].str[-4:]

Is turning your df variable into a Series instead of a Pandas Dataframe. So by the time it gets to the apply statement, it as no idea what you are referring to.

SOLUTION: Use a double brackets to ensure df remains a pandas DataFrame

Setting your df to a series

df = pd.DataFrame([{'a': 1, 'b': 2}, {'a': 1, 'b': 3}])
df = df['a']
type(df)
<class 'pandas.core.series.Series'>

Retaining your df as a DataFrame

df = pd.DataFrame([{'a': 1, 'b': 2}, {'a': 1, 'b': 3}])
df = df[['a']]
type(df)
<class 'pandas.core.frame.DataFrame'>

I think the error is in this line: 'df = Right = df['MED EXP DATE'].str[-4:]' why two assignment operators (=) here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM