应用中含糊的DataFrame

时间:2018-10-23 10:45:26

标签: python pandas dataframe apply

我有以下代码:

def joinOneRow(ser, df):
     temp = df[df.index == 
                max(df.index[df.index <= 
                ser.index[0]])]
     return temp.values[0][0], temp.values[0][1];

def joinWithData(df1, df):
    askTemp, bidTemp = zip(*df1.apply(joinOneRow, args = (df)))

    return askTemp, bidTemp

df1和df中的索引是一个时间戳。 df有2列。 问题是当我想执行以下行时:

askTemp, bidTemp = zip(*trades.apply(ef.joinWithData, args = ob))

我有一个错误:

“ DataFrame的真值不明确。请使用a.empty,a.bool(),a.item(),a.any()或a.all()。”

更新回溯:

Traceback (most recent call last):

File "<ipython-input-5-f74779a26578>", line 1, in <module>
 askTemp, bidTemp = zip(*trades1.apply(ef.joinWithData, args = ob1))

File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 6003, in 
 apply
  kwds=kwds)

File "C:\Anaconda\lib\site-packages\pandas\core\apply.py", line 29, in 
frame_apply
  args=args, kwds=kwds)

File "C:\Anaconda\lib\site-packages\pandas\core\apply.py", line 39, in 
 __init__
  self.args = args or ()

   File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 1573, 
in __nonzero__
   .format(self.__class__.__name__))

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

1 个答案:

答案 0 :(得分:0)

尝试:

askTemp,bidTemp = zip(* trades.apply(ef.joinWithData,args =(ob,))

引用:Pass Dataframe to Apply function pandas as argument