简体   繁体   English

如何在 python 中使用和应用 function 和 pandas 数据帧

[英]how to use and apply function with pandas data frame in python

i have written the function, that could take four input value and produce result based on that我已经编写了 function,它可以采用四个输入值并基于该值产生结果

def python_function(a, b, c, d):
    if [a, b, c, d].count(0) == 4:
        return "NA"

    average = (a + b + c + d) / (4 - [a, b, c, d].count(0))

    # change to a for q1, b for q2, c for q3, d for q4
    if c >= average:
        if c > b:
            return "G"
        else:
            return "S"
    elif c < average:
        return "B"

    return "NA"

calling above function:上面调用 function:

python_function(5.3,9.7,.4,0)

'B'

python_function(5.3,9.7,10.4,0)

'G

However when we are applying the same function for columns of pandas data frame, we are getting errors, i am sure there is a way to do that to handle the float value for logical operator but i am not sure how to do that但是,当我们对 pandas 数据框的列应用相同的 function 时,我们遇到了错误,我确信有一种方法可以处理逻辑运算符的浮点值,但我不知道该怎么做

Data frame:数据框:

   q1_profit    q2_profit   q3_profit   q4_profit
0   89969.7     112896.7    25665.4     0
1   1.6         459.9       295.9       0
2   0.9         9.5         5.3         0
3   1396.1      1105.2      0.2         0
4   17.9        365.5       191.1       0

data_type:数据类型:

q1_profit            1600 non-null float64
q2_profit            1600 non-null float64
q3_profit            1600 non-null float64
q4_profit            1600 non-null int64




 data["rating"] = python_function(data["q1_profit"],data["q2_profit"],data["q3_profit"],data["q4_profit"])

error_messages错误消息

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-51-6dba2870dd9c> in <module>
----> 1 data["rating"] = python_function(data["q1_profit"],data["q2_profit"],data["q3_profit"],data["q4_profit"])

<ipython-input-39-47792387b172> in python_function(a, b, c, d)
      1 def python_function(a, b, c, d):
----> 2     if [a, b, c, d].count(0) == 4:
      3         return "NA"
      4 
      5     average = (a + b + c + d) / (4 - [a, b, c, d].count(0))

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1476         raise ValueError("The truth value of a {0} is ambiguous. "
   1477                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478                          .format(self.__class__.__name__))
   1479 
   1480     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

It looks like you're doing an operation on every row of the dataframe .看起来您正在对dataframe的每一行进行操作。 So I saw the best option to use the apply function.所以我看到了使用apply function 的最佳选择。

input_data = {
     'q1_profit':[89969.7,1.6,0.9,1396.1 ,17.9 ],
     'q2_profit':[112896.7, 459.9,9.5,1105.2 , 365.5],
     'q3_profit' :[25665.4,295.9 ,5.3,0.2, 191.1],
     'q4_profit':[0,0,0,0,0]
      }

import pandas as pd 
data = pd.DataFrame(data=input_data)
 
data['rating'] = data.apply(lambda row: python_function(row["q1_profit"],row["q2_profit"],row["q3_profit"],row["q4_profit"]), axis=1)

print(data)

output: output:

   q1_profit  q2_profit  q3_profit  q4_profit rating
0    89969.7   112896.7    25665.4          0      B
1        1.6      459.9      295.9          0      S
2        0.9        9.5        5.3          0      S
3     1396.1     1105.2        0.2          0      B
4       17.9      365.5      191.1          0      B
data["rating"]  = data.apply(lambda x : python_function(x.q1_profit,x.q2_profit,x.q3_profit,x.q4_profit),
         axis =1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在pandas数据框上应用已定义的函数 - How to apply a defined function on pandas data frame 如何在熊猫数据框上应用Scipy功能 - How to apply scipy function on Pandas data frame Python,将函数应用于数据为参数的熊猫数据框 - Python, apply a function to a pandas data frame where the data is an argument python pandas 在 groupby 中应用 function,并将结果添加为数据框中的列 - python pandas apply function in groupby, and add results as column in data frame 如何使用 apply() 将 Pandas 数据框中的数据更改为小写? - How to use apply() to change data in a pandas data frame to lowercase? 如何在数据框某些行的所有列上使用熊猫套用功能 - How to use pandas apply function on all columns of some rows of data frame 如何应用函数可以使用python-pandas测试数据框中的列表中的元素? - How to apply a function can test whether an element in a list in data frame with python-pandas? 如何使用python-pandas在apply函数中将数据框的索引值作为arg调用? - How to call the index value of the data frame as an arg in apply function with python-pandas? 如何仅在选定的熊猫数据框的行和列上应用功能? - How to apply a function only on selected rows and columns of pandas data frame? 如何使用熊猫将concat函数应用于按数据帧分组? - How to apply a concat function to a group by data frame using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM