Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to use cse insensitive condition in lambda expresion
I want to add 'value' column, and if 'Status' column contains pass(case insensitive) give value is 0 in 'value' column. I use below code, but only giving zero in3rd (but it should give 'zeor" in rows 0,2,3,5.

import pandas as pd

df=pd.DataFrame({'Name':['NHAY', 'HAY','UTY','UIK', 'JUI','YTH'],'Status':['Pass','Running','pass(warning 0)','pass','fail','passed']})

df['value'] = df['Status'].apply(lambda x: '0' if x == 'pass' else '1',case=False)
it give some error:
TypeError: <lambda>() got an unexpected keyword argument 'case'
You don't need to use .apply here, just select appropriate rows and set a new value, e.g.

df.loc[df['Status'].str.contains('pass', case=False), 'value'] = 0
I want to add a new column, for this condition.
(Jul-13-2019, 10:54 AM)SriMekala Wrote: I want to add a new column, for this condition.
I don't understand this, .loc selector automatically creates a new column if needed. In the code above a new column
named value is created. Probably, you need to add a line df.loc[:, 'value'] = df.Status before assigning zero values using the condition.
Just curious, If I do not have a "value" column, and I want to add the column in the same line and else condition:

df=pd.DataFrame({'Name':['NHAY', 'HAY','UTY','UIK', 'JUI','YTH'],'Status':['Pass','Running','pass(warning 0)','pass','fail','passed']})
df['value']=df.loc[df['Status'].str.contains('pass', case=False), 'value'] = 0 # Can I add else condition and new clolumn('value')here?
(Jul-13-2019, 12:14 PM)SriMekala Wrote: df['value']=df.loc[df['Status'].str.contains('pass', case=False), 'value'] = 0
Doing so you encounter with specificity of chained assignment in Python. This line is equivalent to
temporary = 0
df['value'] = temporary
df.loc[...] = temporary
So, you will get a column of zeros. df.loc[..., 'value'] automatically creates a column if it does not exist, you don't need to create it, e.g. by df['value'] or something else.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  lambda functions Gigux 3 427 Feb-17-2019, 06:15 AM
Last Post: scidam
  Formula with elements of list - If-condition regarding the lists elements lewielewis 0 371 Oct-29-2018, 09:32 PM
Last Post: lewielewis
  Splitting values in column in a pandas dataframe based on a condition hey_arnold 1 1,308 Jul-24-2018, 02:18 PM
Last Post: hey_arnold
  if- elif- and condition issue PrateekG 4 780 May-16-2018, 06:44 AM
Last Post: PrateekG
  Newbie question for using map, lambda zydjohn 2 1,050 Dec-09-2017, 07:18 PM
Last Post: zydjohn
  Filter and lambda question smw10c 3 3,768 Apr-27-2017, 04:44 PM
Last Post: zivoni

Forum Jump:

Users browsing this thread: 1 Guest(s)