The attached past string doesn't answer my query. These two are not relevant questions. Those are examples of very simple logics with lambda function using == for a value. In a matching string pattern that won't work. There are plethora of examples with simple == or != cases of lambda but I didn't find a single example for string pattern. I've already googled this on many sites for last 5-6 hours and when I couldn't make anything that's when I asked here.
I've a pandas dataframe with few columns and few rows. I'm trying to achieve a logic using lambda function to look for a string pattern element by element in column B and if pattern matches then cut or copy that cell/element value and paste/overwrite in another column C in same row. At present with lambda I've to use 0 (zero) with else as I can't either think of or found how to pass or don't do anything with column C value for else like pass in python. Without else there is syntax error using lambda.
Below is the dataframe for your reference and the lambda function which is returning the rows I need to look in column B. Replacing values element by element in column C is ending up with ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
Here is the command line I've generated in which I'm trying lambda function to look element by element and if there is a match copy that value to Column C from Column B
df2.C = df2.B.apply(lambda x: x if df2.B.str.contains('z') else 0)
There are other easier ways to achieve what I'm trying, since it is a small dataframe in example, I mean by not using the lambda function. I really would like to see how can we use lambda function here. Other solutions are also welcomed but I'm specifically looking for lambda, as this is what I've learned is a way for beginners to apply logic element by element.
I am looking to generalize this logic and be usable for big datasets and pretty much any search and copy/replace logic for datasets in future by just changing the column data type/property to string before using this logic.
Dataframe:
A B C D E
[-10.01,a,zzz,-11.91,0.53,17.33]
[-12.76,b,zxy,-2.05,14.49,15.10]
[-15.05,c,17.19,1.49,-9.01]
[9.17,d,-10.66,15.35,10.14]
Below is the picture for reference as well.