osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pandas loc on str lower for column comparison


On Friday, 30 August 2019 00:49:32 UTC+10, Piet van Oostrum  wrote:
> Piet van Oostrum  writes:
> 
> > So the correct way to do this is to make df1 a copy rather than a view.
> >
> > df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')]
> 
> Or maybe even make an explicit copy:
> 
> df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
> -- 
> Piet van Oostrum
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]

I have tried both

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()

and 

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()

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.

Sayth