数据框列表理解"zip(...)":仅使用列名称字符串列表即可有效地循环浏览选定的df列 [英] Dataframe list comprehension "zip(...)": loop through chosen df columns efficiently with just a list of column name strings
本文介绍了数据框列表理解"zip(...)":仅使用列名称字符串列表即可有效地循环浏览选定的df列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这只是一个挑剔的句法问题...
This is just a nitpicking syntactic question...
我有一个数据框,我想使用列表推导来评估使用大量列的函数.
I have a dataframe, and I want to use list comprehension to evaluate a function using lots of columns.
我知道我可以做到
df['result_col'] = [some_func(*var) for var in zip(df['col_1'], df['col_2'],... ,df['col_n'])]
我想做这样的事情
df['result_col'] = [some_func(*var) for var in zip(df[['col_1', 'col_2',... ,'col_n']])]
即不必编写 df
n
次.我一生都无法弄清楚语法.
i.e. not having to write df
n
times. I cannot for the life of me figure out the syntax.
推荐答案
这应该可行,但老实说,OP自己也认为,所以+1 OP:)
this should work, but honestly, OP figured it himself as well, so +1 OP :)
df['result_col'] = [some_func(*var) for var in zip(*[df[col] for col in ['col_1', 'col_2',... ,'col_n']])]
这篇关于数据框列表理解"zip(...)":仅使用列名称字符串列表即可有效地循环浏览选定的df列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文