简体   繁体   English

Python:将 lambda 应用于行中列表的每个元素

[英]Python: apply lambda to each element of a list in a row

How can I apply lambda to echa element of list in a row?如何将 lambda 连续应用于列表的 echa 元素?

I have a column with list of 2 elements in each row.我有一列,每行包含 2 个元素的列表。

Those elelemts are strings and I want to make them ints.那些elelemts 是字符串,我想让它们成为整数。

Now: ['111', '222'] Then: [111, 222]现在:['111', '222'] 然后:[111, 222]

How to pass iteration thru each element of a list in a row?如何通过连续列表中的每个元素进行迭代?

df.column.apply(lambda x: int(x) for x in...

And I'm stuck here not knowing how to iterate since I already told lambda to work with x where x is a certain cell.而且我被困在这里不知道如何迭代,因为我已经告诉 lambda 与 x 一起工作,其中 x 是某个单元格。

Thank you in advance.先感谢您。

Data Sample screenshot enclosed.随附数据示例屏幕截图。 enter image description here The problem is with 'coords' column.在此处输入图像描述问题出在“坐标”列。

Have you tried indexing into the list?您是否尝试过索引到列表中?

df.column.apply(lambda x: [int(x[0]), int(x[1])] for x in...

EDIT:编辑:

As your column sometimes contains None, try this:由于您的列有时包含无,请尝试以下操作:

df.column.apply(lambda x: x if pd.isnull(x) else [int(x[0]), int(x[1])]

This assumes that the problem values are None and not something like ['111', None]这假设问题值为 None 而不是 ['111', None]

SOLVED: I don't know why but this worked:已解决:我不知道为什么,但这有效:

df.column.apply(lambda x: (float(x[0]), float(x[1])) if pd.isna([x]).any() == False else x)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM