将函数调用的结果合并到一个数据框中,使用 apply 函数。

huangapple go评论60阅读模式
英文:

Combining results of a function call into a dataframe using apply

问题

以下是您要翻译的代码部分:

import pandas as pd
import yfinance as yf

def get_opt_data(ticker, expiration):
    try:
        data = yf.Ticker(ticker)
        calls = data.option_chain(expiration).calls
        calls['Type'] = 'CALL'
        puts = data.option_chain(expiration).puts
        puts['Type'] = 'PUT'
        combined = pd.concat([calls, puts])
        combined['Ticker'] = ticker
        info = data.info
        combined['Sector'] = info['sector']
        combined['Industry'] = info['industry']
        return combined
    except:
        pass

ticker_list = pd.Series(['AAPL', 'GOOGL', 'MSFT', 'NVDA'])
option_data = ticker_list.apply(lambda x: get_opt_data(x, '2023-07-21'))

请注意,我只翻译了代码部分,没有包括问题的回答。如果您需要更多帮助,请随时提问。

英文:

I am trying to use .apply to create a dataframe containing the concatenated results of the following function which simply pulls option data and puts it into tabular form

import pandas as pd
import yfinance as yf

def get_opt_data(ticker, expiration):
    try:
        data = yf.Ticker(ticker)
        calls = data.option_chain(expiration).calls
        calls['Type'] = 'CALL'
        puts = data.option_chain(expiration).puts
        puts['Type'] = 'PUT'
        combined = pd.concat([calls,puts])
        combined['Ticker'] = ticker
        info = data.info
        combined['Sector'] = info['sector']
        combined['Industry'] = info['industry']
        return combined
    except:
        pass

When I try to pull the data using:

ticker_list = pd.Series(['AAPL','GOOGL','MSFT','NVDA'])
option_data = ticker_list.apply(lambda x: get_opt_data(x, '2023-07-21'))

I get a series containing the resulting dataframes, instead of the dataframes concatenated to create a single dataframe.

I have done something similar in the past that included grouby and worked, I guess I don't understand why the grouby was necessary for it to work. Here is that previous code that combined the results correctly.

grouped_data = raw_data.groupby('Ticker', group_keys=False).apply(lambda x: get_indicators(x))

答案1

得分: 1

这是一个简单的例子。应用变换以1:1的方式进行 - 也就是说,你得到的返回的方框数量与你发送的一样多。你可能想要在每个元素上调用get_opt_data,然后连接结果:

option_data = pd.concat(get_opt_data(x, '2023-07-21') for x in ticker_list)
print(option_data[:5].to_string()) 

产生的结果如下:

        contractSymbol             lastTradeDate  strike  lastPrice     bid     ask     change  percentChange  volume  openInterest  impliedVolatility  inTheMoney contractSize currency  Type Ticker      Sector              Industry
0  AAPL230721C00050000 2023-06-13 19:24:32+00:00    50.0     133.50  134.40  137.35   0.000000       0.000000     1.0          21.0           2.383793        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
1  AAPL230721C00055000 2023-06-16 13:36:40+00:00    55.0     131.17  129.40  132.35  37.170000      39.542550     2.0          23.0           2.218754        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
2  AAPL230721C00060000 2023-06-16 13:36:40+00:00    60.0     126.22  123.20  127.40   1.220001       0.976001     2.0        2540.0           1.773439        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
3  AAPL230721C00065000 2023-06-15 17:56:08+00:00    65.0     121.00  119.45  122.45   0.000000       0.000000     2.0         403.0           1.959473        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
4  AAPL230721C00070000 2023-05-04 14:07:49+00:00    70.0      95.90  110.20  112.10   0.000000       0.000000     4.0         214.0           0.000010        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics

希望这对你有所帮助。

英文:

This is an easy one. Apply transforms things in a 1:1 way - that is, you get the same number of boxes back as you sent in. You probably want to call get_opt_data on each element and concat the results:

option_data = pd.concat(get_opt_data(x, '2023-07-21') for x in ticker_list)
print(option_data[:5].to_string()) 

yields

        contractSymbol             lastTradeDate  strike  lastPrice     bid     ask     change  percentChange  volume  openInterest  impliedVolatility  inTheMoney contractSize currency  Type Ticker      Sector              Industry
0  AAPL230721C00050000 2023-06-13 19:24:32+00:00    50.0     133.50  134.40  137.35   0.000000       0.000000     1.0          21.0           2.383793        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
1  AAPL230721C00055000 2023-06-16 13:36:40+00:00    55.0     131.17  129.40  132.35  37.170000      39.542550     2.0          23.0           2.218754        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
2  AAPL230721C00060000 2023-06-16 13:36:40+00:00    60.0     126.22  123.20  127.40   1.220001       0.976001     2.0        2540.0           1.773439        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
3  AAPL230721C00065000 2023-06-15 17:56:08+00:00    65.0     121.00  119.45  122.45   0.000000       0.000000     2.0         403.0           1.959473        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics
4  AAPL230721C00070000 2023-05-04 14:07:49+00:00    70.0      95.90  110.20  112.10   0.000000       0.000000     4.0         214.0           0.000010        True      REGULAR      USD  CALL   AAPL  Technology  Consumer Electronics


huangapple
  • 本文由 发表于 2023年6月19日 00:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定