Python Pandas - 使用 apply() 时,“Series”对象没有属性“colNames”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40506390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:36:44  来源:igfitidea点击:

Pandas - 'Series' object has no attribute 'colNames' when using apply()

pythonpandas

提问by Runner Bean

I need to use a lambda function to do a row by row computation. For example create some dataframe

我需要使用 lambda 函数进行逐行计算。例如创建一些数据框

import pandas as pd
import numpy as np

def myfunc(x, y):
    return x + y

colNames = ['A', 'B']
data = np.array([np.arange(10)]*2).T

df = pd.DataFrame(data, index=[range(0, 10)], columns=colNames)

using 'myfunc' this does work

使用'myfunc'这确实有效

df['D'] = (df.apply(lambda x: myfunc(x.A, x.B), axis=1))

but this second case does not work!

但这第二种情况不起作用!

df['D'] = (df.apply(lambda x: myfunc(x.colNames[0], x.colNames[1]), axis=1))

giving the error

给出错误

AttributeError: ("'Series' object has no attribute 'colNames'", u'occurred at index 0')

I really need to use the second case (access the colNames using the list) which gives an error, any clues on how to do this?

我真的需要使用第二种情况(使用列表访问 colNames),这会产生错误,有关如何执行此操作的任何线索?

thanks

谢谢

采纳答案by foglerit

When you use df.apply(), each row of your DataFrame will be passed to your lambda function as a pandas Series. The frame's columns will then be the index of the series and you can access values using series[label].

当您使用 时df.apply(),DataFrame 的每一行都将作为熊猫系列传递给您的 lambda 函数。框架的列将成为系列的索引,您可以使用series[label].

So this should work:

所以这应该有效:

df['D'] = (df.apply(lambda x: myfunc(x[colNames[0]], x[colNames[1]]), axis=1))