pandas :我如何将一个列名传递给一个可以在'apply'中使用的函数? [英] Pandas: how can I pass a column name to a function that can then be used in 'apply'?
问题描述
我有一个函数:
pre $ def $ log $ c $ b if row [column_name]> 0.6:
val = 1
elif 0.4< row [column_name]< 0.6:
val = 2
其他:
val = 0
返回val
然后我想在这里使用它:
def create_logit_value(df,name_of_column,d):
df [name_of_column] = df.apply(general_logit,args =(d,),axis = 1)
df [name_of_column +'_category'] = df.apply(logit_categorisation,args =(df [name_of_column ],),axis = 1)
应用这个函数:
create_logit_value(r,the_test_column,{'age': - 。02742,'dlco':0.0053058})
$ c
我得到这个错误(对于create_logit_value函数中的第二行):
<$ ('一个系列的真值不明确,使用a.empty,a.bool(),a.item(),a.any()或a.all()。 ','发生在索引0')
我认为这是因为我正在传递一系列当我调用row [column_name]时,logit_categorisation函数]而不是离散值,但我不知道如何以这种方式提取价值。
可能最好不要这样做。您的函数只使用行和列名称来操作单个值。所以,让你的函数直接接受这个值(也就是你当前调用的值 row [column_name]
)作为它的参数,然后你可以这样做:
df [name_of_column +'_category'] = df [name_of_column] .map(logit_categorization)
I have a function:
def logit_categorisation(row, column_name):
val = 0
if row[column_name] > 0.6:
val = 1
elif 0.4 < row[column_name] < 0.6:
val = 2
else:
val = 0
return val
I then want to use this in:
def create_logit_value(df, name_of_column, d):
df[name_of_column] = df.apply(general_logit, args=(d,), axis=1)
df[name_of_column + '_category'] = df.apply(logit_categorisation, args=(df[name_of_column],), axis=1)
And apply the function like this:
create_logit_value(r, "the_test_column", {'age':-.02742, 'dlco': 0.0053058 })
I get this error (for the second line in the create_logit_value function):
('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
And I think this is because I am passing a series in the logit_categorisation function when I call row[column_name] rather than a discrete value but I don't know how the extract the value in this way.
Probably better not to do it that way. Your function only uses the row and column name to operate on a single value. So just make your function accept the value directly (that is, the value you currently call row[column_name]
) as its argument, and then you can do:
df[name_of_column + '_category'] = df[name_of_column].map(logit_categorization)
这篇关于 pandas :我如何将一个列名传递给一个可以在'apply'中使用的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!