简体   繁体   English

Python:如何使用 lambda function 返回特定值?

[英]Python : How to use lambda function to return a specific value?

I have a dataframe with a column "A" whose values can be as such:我有一个 dataframe 列“A”,其值可以是:

[';', '', 'M;', 'M', ';M', 'M;M', ';;M']

I would like to return the value 'M' in my second column "B" if I can find "M" (it doesn't matter if there are several M's) in the row on column A. If there is no M, the row in B should stay empty.如果我可以在 A 列的行中找到“M”(是否有多个 M 无关紧要),我想在第二列“B”中返回值“M”。如果没有 M,则B 中的行应保持为空。 I'm having trouble translating this into a lambda function.我无法将其翻译成 lambda function。 Can anyone help please?有人可以帮忙吗?

df['B']=df['A'].apply(lambda x:x[....... if 'M' in x else None])

Just use the expression you want to return before the if keyword:只需在if关键字之前使用要返回的表达式:

df['B']=df['A'].apply(lambda x:'M' if 'M' in x else None)

The keys to understand this is that "lambda" will resolve a single expression, and return its value, and the inline "... if... else..." construct in Python just evaluates to the first or the last part depending on the test expression.理解这一点的关键是“lambda”将解析单个表达式,并返回其值,而 Python 中的内联“... if... else ...”构造仅计算为第一部分或最后一部分,具体取决于在测试表达式上。

If you were not using apply in a single column, but on the whole dataframe (if you'd need to check values in more than one column, for example), you have to pass the "axis=1" parameter to apply.如果您没有在单个列中使用 apply ,而是在整个 dataframe 上使用(例如,如果您需要检查多列中的值),则必须传递“axis = 1”参数才能应用。 In that case, though not needed, it would be nice to have an extra pair of parentheses around the lambda.在这种情况下,虽然不需要,但最好在 lambda 周围有一对额外的括号。 Otherwise the burden falls on the readers of your code to determine if the "," separating the lambda body from the next argument is really a separator or part of the lambda:否则,代码的读者有责任确定将 lambda 主体与下一个参数分开的“,”是否真的是 lambda 的分隔符或一部分:

df['B']=df.apply((lambda row:'M' if 'M' in row['A'] else None), axis=1)

You do not need to use a lambda function.您不需要使用 lambda function。 Avoid the loop and use loc with str.contains避免循环并将locstr.contains一起使用

df = pd.DataFrame({'A': [';', '', 'M;', 'M', ';M', 'M;M', ';;M']})
df.loc[df['A'].str.contains('M'), 'B'] = 'M'

     A    B
0    ;  NaN
1       NaN
2   M;    M
3    M    M
4   ;M    M
5  M;M    M
6  ;;M    M

add parameter axis , or the function will be applied to every column.添加参数axis ,否则 function 将应用于每一列。 And that will make the code like:这将使代码如下:

df["B"] = df.apply(lambda x: 'M' if ('M' in x['A']) else None ,axis=1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python lambda 返回值 - Python lambda return value 我可以使用 python return function 只返回值到具体的 function - Can I use python return function to only return the value to the specific function 如何告诉 Python 中的 lambda function 返回 None 而不是抛出键值错误 - How to tell my lambda function in Python to Return None instead of throwing up Key Value error 为什么我不能在 python 的 lambda 函数中使用“return”? - Why can't I use “return” in lambda function in python? 将没有返回值的 Python Lambda 函数转换为 Pyspark - Transforming Python Lambda function without return value to Pyspark 如何将绘图分配给变量并将该变量用作 Python 函数中的返回值 - How to assign a plot to a variable and use the variable as the return value in a Python function Python 3如何正确返回函数中的值以供以后使用 - Python 3 how to properly return a value in function for later use 如何使用函数的返回值作为在python中返回元组的条件 - How to use return value of a function as condition of while that returns tuple in python 如何使用python apply/lambda/shift函数根据2列的值获取该特定列的前一行值? - How to use python apply/lambda/shift function to get the previous row value of that particular column based on the value of 2 columns? 如何使用 get function 将参数/参数传递给 lambda function 这是 Z23DFC6EEEB744 中字典中的值 - how to use get function to pass an argument/parameter to a lambda function which is a value in the dictionary in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM