pandas loc on str lower for column comparison
Sayth Renshaw
flebber.crue at gmail.com
Tue Sep 10 00:04:24 EDT 2019
On Tuesday, 10 September 2019 12:56:36 UTC+10, Sayth Renshaw wrote:
> 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
This works on an individual row
df2 = df1.loc[(df1['Current Team'] == df1['New Team']),'D'] = 'Wow'
But how do I apply it to the whole new column and return the new dataset?
Trying to use lambda but it cannot contain assigment
df2 = df1.apply(lambda df1: [ (df1['Current Team'] == df1['New Team']) ]['D'] = 'succeed')
df2
Confused
Sayth
More information about the Python-list
mailing list