【问题标题】:pandas apply function with if statement inside the custom functionpandas 在自定义函数中使用 if 语句应用函数
【发布时间】:2022-06-10 22:42:09
【问题描述】:
def cal_properties(pressure):
    
    if pressure>=0 and pressure<=1000:
        density=1/pressure  #myfunction(pressure)
    else:
        density=pressure*10

    return  density

print(df)


WELL_NKNME  10A74  10A75  10A77  10A78  11A74  11A75  11A77  11A78
Date                                                              
2022-06-05    0.0    0.0    0.0    0.0    0.0  122.8   56.3   96.3
2022-06-06    0.0    0.0    0.0    0.0    0.0  118.3   52.0   85.3
2022-06-07    0.0    0.0    0.0    0.0    0.0  119.5   52.9   87.4

df=df.apply(lambda row: cal_properties(row),axis=1)

然后我得到一个与 if 语句相关的错误


----> 7 df=df.apply(lambda row: cal_properties(row),axis=1)
      8 df

C:\Anaconda\envs\dash_tf\lib\site-packages\pandas\core\frame.py in apply(
    self,
    func,
    axis,
    raw,
    result_type,
    args,
    **kwargs
)
   8738             kwargs=kwargs,
   8739         )
-> 8740         return op.apply()
   8741 
   8742     def applymap(

C:\Anaconda\envs\dash_tf\lib\site-packages\pandas\core\apply.py in apply(self)
    686             return self.apply_raw()
    687 
--> 688         return self.apply_standard()
    689 
    690     def agg(self):

C:\Anaconda\envs\dash_tf\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    810 
    811     def apply_standard(self):
--> 812         results, res_index = self.apply_series_generator()
    813 
    814         # wrap results

C:\Anaconda\envs\dash_tf\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
    826             for i, v in enumerate(series_gen):
    827                 # ignore SettingWithCopy here in case the user mutates
--> 828                 results[i] = self.f(v)
    829                 if isinstance(results[i], ABCSeries):
    830                     # If we have a view on v, we need to make a copy because

C:\Temp\1\ipykernel_840\3896317687.py in <lambda>(row)
      5 # print(df.iloc[0:1,:])
      6 # print(df.to_dict())
----> 7 df=df.apply(lambda row: cal_properties(row),axis=1)
      8 df

C:\Temp\1\ipykernel_840\2054338456.py in cal_properties(pressure)
      1 def cal_properties(pressure):
      2 
----> 3     if pressure>=0 and pressure<=1000:
      4         density=1/pressure  #myfunction(pressure)
      5     else:

C:\Anaconda\envs\dash_tf\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1535     @final
   1536     def __nonzero__(self):
-> 1537         raise ValueError(
   1538             f"The truth value of a {type(self).__name__} is ambiguous. "
   1539             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."

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

这里是数据框字典数据,因此您可以练习代码。如果我没有 if 语句,则代码很好。我不确定如何解决?感谢您的帮助。

print(df.to_dict())

{'10A74': {Timestamp('2022-06-05 00:00:00'): 0.0, Timestamp('2022-06-06 00:00:00'): 0.0, Timestamp('2022-06-07 00:00:00'): 0.0}, '10A75': {Timestamp('2022-06-05 00:00:00'): 0.0, Timestamp('2022-06-06 00:00:00'): 0.0, Timestamp('2022-06-07 00:00:00'): 0.0}, '10A77': {Timestamp('2022-06-05 00:00:00'): 0.0, Timestamp('2022-06-06 00:00:00'): 0.0, Timestamp('2022-06-07 00:00:00'): 0.0}, '10A78': {Timestamp('2022-06-05 00:00:00'): 0.0, Timestamp('2022-06-06 00:00:00'): 0.0, Timestamp('2022-06-07 00:00:00'): 0.0}, '11A74': {Timestamp('2022-06-05 00:00:00'): 0.0, Timestamp('2022-06-06 00:00:00'): 0.0, Timestamp('2022-06-07 00:00:00'): 0.0}, '11A75': {Timestamp('2022-06-05 00:00:00'): 122.8, Timestamp('2022-06-06 00:00:00'): 118.3, Timestamp('2022-06-07 00:00:00'): 119.5}, '11A77': {Timestamp('2022-06-05 00:00:00'): 56.3, Timestamp('2022-06-06 00:00:00'): 52.0, Timestamp('2022-06-07 00:00:00'): 52.9}, '11A78': {Timestamp('2022-06-05 00:00:00'): 96.3, Timestamp('2022-06-06 00:00:00'): 85.3, Timestamp('2022-06-07 00:00:00'): 87.4}}

【问题讨论】:

  • 为什么要写一个函数,然后把它放到另一个 lambda 函数中应用?只需应用您编写的函数
  • 还有你的意思是申请整个df?还是只是系列之一?
  • 你打电话给cal_properties(row),所以pressure是一整行。那么if pressure&gt;=0 and pressure&lt;=1000 是什么意思呢?
  • 如果您的意图是将函数应用于每个单元格,请使用df.applymap。否则,该函数将被传递一行 (axis=1),异常跟踪非常不言自明

标签: python pandas


猜你喜欢
  • 1970-01-01
  • 2014-10-14
  • 2012-12-21
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 2019-08-30
  • 2012-02-26
  • 2022-07-29
相关资源
最近更新 更多