pandas loc on str lower for column comparison
Sayth Renshaw
flebber.crue at gmail.com
Mon Sep 9 22:56:23 EDT 2019
On Friday, 6 September 2019 07:52:56 UTC+10, Piet van Oostrum wrote:
> Piet van Oostrum <> writes:
>
> > That would select ROWS 0,1,5,6,7, not columns.
> > To select columns 0,1,5,6,7, use two-dimensional indexes
> >
> > df1 = df.iloc[:, [0,1,5,6,7]]
> >
> > : selects all rows.
>
> And that also solves your original problem.
>
> This statement:
>
> df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == df1.loc['New Team'].str.lower().str.strip()
>
> should not use .loc, because then you are selecting rows, not columns, but:
>
> df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip()
> --
> Piet van Oostrum <>
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]
That actually creates another error.
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
So tried this
df['c'] = df.apply(lambda df1: df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip(), axis=1)
Based on this SO answer https://stackoverflow.com/a/46570641
Thoughts?
Sayth
More information about the Python-list
mailing list