lambda功能带有循环和条件
我想在所有行中处理PANDAS DataFrame的列。
示例:
df_column:
0 urewjf, abc, urewurwe
1 fdsfs, kjf, oirpiewpoirew...
2 ab, yryrewy, popof...
我想处理的值:
- 在具有Len< 4示例的字符串开头添加空白:'abc','ab'
我试图将每个行/系列转换为字符串,然后运行lambda函数将其应用于每一行:
list(map(lambda x: (x.rjust(5) for x in df.column if len(x) < 4), df.column))
它不起作用。
另外,由于系列类型,我使用.apply()时会遇到问题,但是显然我的lambda功能未正确构建。
有什么想法吗? 先感谢您
I have a column in Pandas DataFrame which I would like to process for all rows.
Example:
df_column:
0 urewjf, abc, urewurwe
1 fdsfs, kjf, oirpiewpoirew...
2 ab, yryrewy, popof...
What I want to do with thouse values:
- add blank space in the beginning of the strings which have len<4 EXAMPLE: ' abc', ' ab'
I was trying to convert each row/Series into string and then run lambda function to apply it for each row:
list(map(lambda x: (x.rjust(5) for x in df.column if len(x) < 4), df.column))
It was not working.
Also, I have issues when I use .apply() because of the Series type, but apparantly my lambda function is not constructed correctly.
Any ideas?
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以只使用Pandas矢量化的字符串方法:
You can just use a pandas vectorised string methods: