Python 熊猫:如何对每一行应用转换?

Python 熊猫:如何对每一行应用转换?,python,pandas,Python,Pandas,最初我有一行,我想按照一些非平凡的算法为每一行设置一些新的列 我可以这样做: for index, row in df.iterrows(): df.loc[df.index == index, 'NEW_COL'] = ... 但它相当笨拙。有没有办法将lambda row->row定义为一行,并将其应用于数据帧?我认为您需要类似以下内容: def func(row): row.(here you can access any column of your dataframe

最初我有一行,我想按照一些非平凡的算法为每一行设置一些新的列

我可以这样做:

for index, row in df.iterrows():
    df.loc[df.index == index, 'NEW_COL'] = ...

但它相当笨拙。有没有办法将lambda row->row定义为一行,并将其应用于数据帧?

我认为您需要类似以下内容:

def func(row):
    row.(here you can access any column of your dataframe) 

    return (the value in here will go to the 'NEW_COL' you are defining)

df['NEW_COL'] = df.apply(func,axis=1)

如果您想要更具体的内容,请在帖子中提供更多详细信息

您的意思是
df.apply(lambda行:func(row),axis=1)
?下一行是否依赖于前一行的新列?非平凡算法的简化版本将有助于。。。除非@EdChum已经回答了您的问题。
df.apply
选项只适用于只需要一个数据列的情况。你能给我们一个关于这个非平凡算法的提示吗?