如何使用Apply函数从Pandas返回多个列

2024-09-30 01:19:51 发布

您现在位置:Python中文网/ 问答频道 /正文

长期读者第一次海报。 自学Python,通常会设法用蛮力解决问题或在这里找到答案,但我已经坚持了好几天了

我有一个功能:

def Main(row):

LessThanPiv = HighPiv.iloc[np.where((HighPiv.High.values > row.Close) & (HighPiv.index.values < row.name))]

    Less_Than_Piv = pd.DataFrame()

    counter = 1
    a = 1
    while counter ==1:
        if LessThanPiv.empty:
            break
        FirstBroken = LessThanPiv.head(a)
        BrokenPivRows = df.iloc[np.where((df.index.values > FirstBroken.index.values) & (df.index.values <= row.name))]
        BrokenPivs = BrokenPivRows.iloc[np.where(BrokenPivRows.Close.values < FirstBroken.High.values)]
        Less_Than_Piv = LessThanPiv.append(BrokenPivs)
        a += 1
        if len(FirstBroken) == 1:
            break
    if Less_Than_Piv.empty:
        ClosestPiv = HighPiv.iloc[(HighPiv.index.values < row.name).astype(float)]
        ClosestPiv = ClosestPiv.tail(1)
        ClosestPiv_Volume = ClosestPiv.Volume.values
        ClosestPiv_Close = ClosestPiv.Close.values
        ClosestPiv_Open = ClosestPiv.Open.values
        ClosestPiv_Date = ClosestPiv.Date.values
        ClosestPiv = ClosestPiv.High.values
    else:
        ClosestPiv = df.iloc[(Less_Than_Piv.High.sub(row.Close).abs().idxmin())]
        ClosestPiv_Volume = ClosestPiv.Volume
        ClosestPiv_Close = ClosestPiv.Close
        ClosestPiv_Open = ClosestPiv.Open
        ClosestPiv_Date = ClosestPiv.Date
        ClosestPiv = ClosestPiv.High
    if row.Close < ClosestPiv:
        BreakOutDay = Next_Day(row, 1)
        TradeDay = Next_Day(BreakOutDay, 1)
        if BreakOutDay.Close > ClosestPiv:
            if row.Volume > (3 * Average_Volume) and row.Close > row.Open or ClosestPiv_Volume > (3 * Average_Volume) and ClosestPiv_Close > ClosestPiv_Open:
                if row.Volume > (3 * Average_Volume) and row.Close > row.Open:
                    data['Spike Day'] = row.Date
                    data['Average Volume'] = [Average_Volume]
                else:
                    data['Spike Day Closest Piv'] = [ClosestPiv_Date]
                    data['Average Volume'] = [Average_Volume]

            TradeBuy = TradeDay['Open']
            FollDays = df.iloc[np.where(df.index.values >= TradeDay.name)]
            FallBack = FollDays.iloc[np.where(FollDays.Low < FollDays.Low.shift())]
            PulledBack = FallBack.iloc[np.where(FallBack.Close < (FallBack.apply(PullBack, Foll=FollDays, axis=1)))]

            TradeSell = PulledBack.head(1)

            if PulledBack.empty:
                TradeSell = df.tail(1)

            data['Ticker'] = [i]
            data['Breakout Date'] = BreakOutDay['Date']
            Data = data['Breakout Date']
            Win = TradeSell.Open.values - TradeBuy
            data['Profit'] = [Win]
            Profit = data['Profit']
            data['Buy Price'] = [TradeBuy]
            data['Sell Date'] = [TradeSell.Date.values]

            return [Ticker], [Profit]

我叫它和你一起去

row[['Ticker', 'Profit']] = row.apply(lambda x:Main(x), axis=1, result_type='expand')

无论我尝试什么,我似乎都无法将“数据”的结果放入新的数据帧或现有的数据帧中。我试过:

return pd.Series([Ticker], [Profit])

但我总是犯同样的错误

ValueError:使用iterable设置时,必须具有相等的len键和值

我知道这是我做错了,但我就是不知道是什么


Tags: dfclosedatadateindexifnpopen

热门问题