Reputation: 1019
I have a dataframe with multiple columns that I imported like this:
Flash_Bookings = pd.read_excel(Bookings.xlsx',sheetname='FLASH Bookings'
,index=False, encoding='utf-8')
Then I convert them to string, int or datetime format depending on what they are:
Flash_Bookings['BookingDate'] = pd.to_datetime(Flash_Bookings['BookingDate'],
format='%Y-%m-%d')
Flash_Bookings['ContractNo'] = Flash_Bookings['ContractNo'].astype(str)
Flash_Bookings['Rep'] = Flash_Bookings['Rep'].astype(str)
They all work fine (I have other columns in there as well that I didn't include here that all were good except for one "Rep" so the error message I get is:
KeyError: 'Rep'
I even ran:
Flash_Bookings.dtypes
and get:
BookingDate datetime64[ns]
ContractNo object
Rep object
so it works perfectly fine for ContractNo which is a string just like Rep.
Any ideas why Rep would not work and how I can fix it?
Upvotes: 1
Views: 1376
Reputation: 82785
You probably have an extra space in the header
Try:
Flash_Bookings.columns = Flash_Bookings.columns.str.strip()
Then call
Flash_Bookings['Rep'] = Flash_Bookings['Rep'].astype(str)
Upvotes: 1