osdir.com


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

pandas loc on str lower for column comparison


Sayth Renshaw <flebber.crue at gmail.com> writes:

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

df1['Difference'] = df1['Current Team'].str.lower().str.strip() ==
                    df1['New Team'].str.lower().str.strip()

works on whole columns, not only on an individual row.


xls = pd.ExcelFile("Melbourne.xlsx")
df = xls.parse('Sheet1', skiprows= 4)
df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip()
print(df1.head())

-- 
Piet van Oostrum <piet-l at vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]