[英]Why does the lambda instruction return different results?
import pandas as pd
df = pd.DataFrame([['AB,CD'],['AB,FJS'],['DG']],
index = [1,2,3],columns=['A'])
Why do the following two codes return different results为什么以下两个代码返回不同的结果
for i in df["A"]:
for n in i.split(","):
print(n)
which returns返回
AB
CD
AB
FJS
DG
but the other code但其他代码
def fx(x):
for i in x["A"]:
for n in i.split(","):
print(n)
df.apply(lambda x: fx(x), axis = 1)
returns返回
A
B
,
C
D
A
B
,
F
J
S
D
G
So, why does lambda return different results?那么,为什么 lambda 会返回不同的结果呢? And how can I change the lambda code to return the same results as the previous one?
以及如何更改 lambda 代码以返回与前一个相同的结果? Thanks in previous
感谢之前的
The results are different because lambda function is applied to each row separately.结果不同,因为 lambda 函数分别应用于每一行。 So, for example, in the first row
x
is AB,CD
and when you write for i in x["A"]
, you iterate over each separate symbol.因此,例如,在第一行中
x
是AB,CD
并且当您for i in x["A"]
,您会遍历每个单独的符号。
def fx(x):
for i in x["A"].split(","):
print(i)
df.apply(lambda x: fx(x), axis = 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.