[英]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
避免循环并将loc
与str.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.