Pandas 将函数应用于列

我在将多个函数应用于我的数据框时遇到一些问题。


我创建了一个示例代码来说明我正在尝试做什么。可能有比我正在做的方式更好的方法来完成这个特定的功能,但我试图为我的问题找到一个通用的解决方案,因为我使用了多个函数,而不仅仅是如何最有效地完成这个特定的事情。


基本上,我有一个如下所示的示例数据框(df1):


   Ticker      Date  High  Volume

0    AAPL  20200501   1.5     150

1    AAPL  20200501   1.2     100

2    AAPL  20200501   1.3     150

3    AAPL  20200502   1.4     130

4    AAPL  20200502   1.2     170

5    AAPL  20200502   1.1     160

6    TSLA  20200501   2.5     250

7    TSLA  20200501   2.2     200

8    TSLA  20200501   2.3     250

9    TSLA  20200502   2.4     230

10   TSLA  20200502   2.2     270

11   TSLA  20200502   2.1     260

和一个如下所示的示例数据框(df2):


  Ticker      Date  Price  SumVol

0   AAPL  20200508    1.2       0

1   TSLA  20200508    2.2       0

df2 中“SumVol”列中的值应填充 df1 中“Volume”列中值的总和,直到第一次在 df2 中看到“Price”(df1) 列中的值为止,并且df1 中的日期与 df2 中的日期匹配


期望的输出:


    Ticker      Date  Price  SumVol

0   AAPL  20200508    1.2    300

1   TSLA  20200508    2.2    500

由于某种原因,我无法获得此输出,因为我可能在尝试将该函数应用于数据帧的代码行中做错了什么。我希望这里有人可以帮助我。


完整的示例代码,包括示例数据帧:


import pandas as pd


df1 = pd.DataFrame({'Ticker': ['AAPL', 'AAPL', 'AAPL', 'AAPL', 'AAPL', 'AAPL', 'TSLA', 'TSLA', 'TSLA', 'TSLA', 'TSLA', 'TSLA'],

                'Date': [20200501, 20200501, 20200501, 20200502, 20200502, 20200502, 20200501, 20200501, 20200501, 20200502, 20200502, 20200502],

               'High': [1.5, 1.2, 1.3, 1.4, 1.2, 1.1, 2.5, 2.2, 2.3, 2.4, 2.2, 2.1],

                'Volume': [150, 100, 150, 130, 170, 160, 250, 200, 250, 230, 270, 260]})

print(df1)


df2 = pd.DataFrame({'Ticker': ['AAPL', 'TSLA'],

               'Date': [20200501, 20200502],

                'Price': [1.4, 2.2],

                'SumVol': [0,0]})


print(df2)


def VolSum(ticker, date, price):

    df11 = pd.DataFrame(df1)

    df11 = df11[df11['Ticker'] == ticker]

    df11 = df11[df11['Date'] == date]

    df11 = df11[df11['High'] < price]


    df11 = pd.DataFrame(df11)

    return df11.Volume.sum


df2['SumVol'].apply(VolSum(df2['Ticker'], df2['Date'], df2['Price']), inplace=True).reset_index(drop=True, inplace=True)

print(df2)


海绵宝宝撒
浏览 74回答 1
1回答

宝慕林4294392

失败的第一个原因是你的函数以&nbsp;return df11.Volume.sum(不带括号)结尾,因此你只返回sum函数,而不是其执行结果。另一个原因是您可以将函数应用于 Dataframe 的每一行,但必须传递axis=1参数。但是之后:要应用的函数应该有一个参数 - 当前行,其结果可以替换到所需的列下。失败的第三个原因是df2包含df1中不存在的日期,因此您不可能找到任何匹配的行。如何获得预期结果 - 方法1首先,df2必须包含可能与df1匹配的值。我将df2定义为:&nbsp; Ticker&nbsp; &nbsp; &nbsp; Date&nbsp; Price&nbsp; SumVol0&nbsp; &nbsp;AAPL&nbsp; 20200501&nbsp; &nbsp; 1.4&nbsp; &nbsp; &nbsp; &nbsp;01&nbsp; &nbsp;TSLA&nbsp; 20200502&nbsp; &nbsp; 2.3&nbsp; &nbsp; &nbsp; &nbsp;0然后我将你的功能更改为:def VolSum(row):&nbsp; &nbsp; df11 = pd.DataFrame(df1)&nbsp; &nbsp; df11 = df11[df11['Ticker'] == row.Ticker]&nbsp; &nbsp; df11 = df11[df11['Date'] == row.Date]&nbsp; &nbsp; df11 = df11[df11['High'] < row.Price]&nbsp; &nbsp; return df11.Volume.sum()最后我生成的结果为:df2['SumVol'] = df2.apply(VolSum, axis=1)结果是:&nbsp; Ticker&nbsp; &nbsp; &nbsp; Date&nbsp; Price&nbsp; SumVol0&nbsp; &nbsp;AAPL&nbsp; 20200501&nbsp; &nbsp; 1.4&nbsp; &nbsp; &nbsp;2501&nbsp; &nbsp;TSLA&nbsp; 20200502&nbsp; &nbsp; 2.3&nbsp; &nbsp; &nbsp;530如何获得预期结果——方法2但更简洁优雅的方法是将求和函数定义为:def VolSum2(row):&nbsp; &nbsp; return df1.query('Ticker == @row.Ticker and '&nbsp; &nbsp; &nbsp; &nbsp; 'Date == @row.Date and High < @row.Price').Volume.sum()并以同样的方式应用它:df2['SumVol'] = df2.apply(VolSum2, axis=1)结果当然是一样的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python