pandas loc on str lower for column comparison

Sayth Renshaw flebber.crue at gmail.com
Sat Aug 31 20:37:36 EDT 2019


On Sunday, 1 September 2019 05:19:34 UTC+10, Piet van Oostrum  wrote:
> Sayth Renshaw writes:
> 
> > But on both occasions I receive this error.
> >
> > # KeyError: 'the label [Current Team] is not in the [index]'
> >
> > if I test df1 before trying to create the new column it works just fine.
> >
> What do you mean by testing df1?
> 
> And could it be that 'Current Team' is spelled differently in the assignment than in the construction of df1? For example a difference in spaces, like a triling space or a breaking vs. non-breaking space? Please check that both are exactly the same.
> 
> -- 
> Piet van Oostrum 
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]

Hi

Version info

1.1.0 #scipy
0.23.4 #pandas
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 35 entries, 0 to 34
Data columns (total 5 columns):
UID             35 non-null object
Name            35 non-null object
New Leader      35 non-null object
Current Team    35 non-null object
New Team        35 non-null object
dtypes: object(5)
memory usage: 1.4+ KB

I had to anonymise the sheet. But same structure. tested produces same errors.

import pandas as pd
import scipy

print(scipy.version.version)
print(pd.__version__)
xls = pd.ExcelFile("Melbourne-anon.xlsx")
df = xls.parse('Sheet1', skiprows= 4)
# df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')]
# print(df['Current Team'] == df1['Current Team'])
df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')] 
# df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == df1.loc['New Team'].str.lower().str.strip()
# df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == df1.loc['New Team'].str.lower().str.strip()
df1.info()





More information about the Python-list mailing list