Try
lambda x: 1 if x == "C" else 0
possible duplicate of Is there a way to perform "if" in python's lambda
Example :
map(lambda x: True if x % 2 == 0 else False, range(1, 11))
result will be - [False, True, False, True, False, True, False, True, False, True]
Answer from pnv on Stack OverflowTry
lambda x: 1 if x == "C" else 0
possible duplicate of Is there a way to perform "if" in python's lambda
Example :
map(lambda x: True if x % 2 == 0 else False, range(1, 11))
result will be - [False, True, False, True, False, True, False, True, False, True]
It will be simpler to just do this:
df["Cherbourg"] = (df["Embarked"] == "C").astype('int)
You'll have to wrap the map around a filter around the list:
example_map = map(lambda x: x*2, filter(lambda x: x*2/6. != 1, range(5)))
Alternatively, you could filter your map rather than maping your filter.
example_map = filter(lambda x: x/6. != 1, map(lambda x: x*2, range(5)))
Just remember that you're now filtering the RESULT rather than the original (i.e. lambda x: x/6. != 1 instead of lambda x: x*2/6. != 1 since x is already doubled from the map)
Heck if you really want, you could kind of throw it all together with a conditional expression
example_map = map(lambda x: x*2 if x*2/6. != 1 else None, range(5))
But it'll leave you with [0, 2, 4, None, 8]. filter(None, example_map) will drop the Nones and leave you [0, 2, 4, 8] as expected.
dictionary - Python - How to use map function and lambda function with if condition - Stack Overflow
Usage of map function correctly with an if statement for python - Stack Overflow
python - Lambda with if condition in map() - Stack Overflow
python - How to use map function with condition? - Stack Overflow
>>> lst = [1,2,4,5]
>>> map(lambda x: 'lower' if x < 3 else 'higher', lst)
['lower', 'lower', 'higher', 'higher']
Aside: It's usually preferred to use a list comprehension for this
>>> ['lower' if x < 3 else 'higher' for x in lst]
['lower', 'lower', 'higher', 'higher']
Ternary operator:
map(lambda x: 'lower' if x<3 else 'higher', lst)
map always produces one output item for each input item, it can not remove elements. Furthermore, map should not be used to mutate objects, that's not its job, and because it's lazy the results can be unexpected.
filter is designed to create an output with less elements than the input, although it's mostly useful if you already have a ready-made predicate (filtering) function.
Since you do not, you can and should use comprehensions which provide a relatively terse way to perform iteration, filtering, mapping and collection in a single construct:
wordlist = ['hello','world','Tom']
checklist = ['hello','world']
print('before')
print(wordlist)
wordlist = [word for word in wordlist if word not in checklist]
print('after')
print(wordlist)
ps: if you want to modify things in-place, use a regular loop
You can try this instead, which doesn't use lambda but accomplishes your goal. Let us know if you absolutely must use lambda. The issue with your lambda expression is that your modifying the list that you are providing to map in the lambda function.
wordlist_2 [word for word in wordlist if word not in checklist and word]
The last and word is to not add None to your list.
pass is a statement, but inline if, being an operator, needs its operands to be expressions. map can’t actually remove elements from the sequence, but filter (returns a new list with only the values for which the function returns True) can:
print filter(lambda x: str(x)[-1] == '2', even)
If you're like me and don't like filters and lambda, you can accomplish this with Python list comprehension:
print [x for x in even if str(x)[-1] == '2']
writing an if statement with a lambda function:
I'm trying to filter a map in spark using an if statement but get a syntax error. I have not been able to find the error. Can you guys tell me what I am doing wrong here?
full_count_with0val = full_rdd.map(lambda x: json.loads(x[1])).flatMap(lambda x: x['exposures']).map(lambda x: x['pdd_list'] if len(x['pdd_list'])==0).take(5)