Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combining results of a function call into a dataframe using apply

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))

>Solution :

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


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading