Search code examples
pythonpandasdataframemerge

Applying function to Pandas Data frame and merging resulting data frames into a new data frame


I have this code:

import pandas as pd
import yfinance as yF
import datetime
from functools import reduce


def get_returns_change_period(tic,com,OHLC_COL1,OHLC_COL2):
    df_Stock = yF.download(tickers = tic, period = "38y", interval = "1mo", prepost = False, repair = False)
    df_Stock['MONTH'] = pd.to_datetime(df_Stock.index)
    df_Stock = df_Stock.sort_values(by='MONTH')
    
    df_Stock[com + ' % Change '+'3M'] = (((df_Stock[OHLC_COL1].shift(2) - df_Stock[OHLC_COL2]))/df_Stock[OHLC_COL2]) *100          
    df_Stock[com + ' % Change '+'3M'] = (df_Stock[com + ' % Change '+'3M'] * 100 )/df_Stock[OHLC_COL2]
    
    
    df_Stock[com + ' % Change '+'2M'] = (((df_Stock[OHLC_COL1].shift(1) - df_Stock[OHLC_COL2]))/df_Stock[OHLC_COL2]) *100          
    df_Stock[com + ' % Change '+'2M'] = (df_Stock[com + ' % Change '+'2M'] * 100 )/df_Stock[OHLC_COL2]
    
    
    df_Stock[com + ' % Change '+'M'] = (((df_Stock[OHLC_COL1].shift(0) - df_Stock[OHLC_COL2]))/df_Stock[OHLC_COL2]) *100          
    df_Stock[com + ' % Change '+'M'] = (df_Stock[com + ' % Change '+'M'] * 100 )/df_Stock[OHLC_COL2]
    
          
    return df_Stock.filter(regex='MONTH|% Change')

Everything works as expected until this point.

get_returns_change_period('XOM','EXXON MOBIL','High','Open')

Now I am trying to Apply Lambda function to this function.

df_Industry = pd.DataFrame({'ID':['1', '2'], 'Ticker': ['AIN', 'TILE'], 'Company':['Albany International', 'Interface']})


df1 = df_Industry.apply(lambda x: get_returns_change_period(x.Ticker, x.Company, 'High','Open'), axis=1)

#df1 = df1.T
df1

The output has to be data frame which is merged result of multiple stocks which are returned by get_returns_change_period() function.

The problem/solution here is very similar to this one. But I am unable to reuse the solution given. Returns for multiple time periods are calculated and returned by the function here:

get_returns_change_period()
  

Solution

  • If you want to merge the dataframes computed by each return of the get_returns_change_period function, you can use the concat function of pandas in this way:

    merged_df = pd.concat(df1.tolist(), axis=1)
    

    Here's the output you get on a sample of your data (the last 5 observations):

    Date MONTH Albany International % Change 3M Albany International % Change 2M Albany International % Change M MONTH Interface % Change 3M Interface % Change 2M Interface % Change M
    2023-06-01 00:00:00 2023-06-01 00:00:00 10.1677 11.6873 14.0497 2023-06-01 00:00:00 271.476 235.975 400.949
    2023-07-01 00:00:00 2023-07-01 00:00:00 1.14388 3.13983 5.61434 2023-07-01 00:00:00 -92.5231 10.4251 140.739
    2023-08-01 00:00:00 2023-08-01 00:00:00 -1.31092 0.967072 0.92409 2023-08-01 00:00:00 -90.5255 15.9752 90.5256
    2023-09-01 00:00:00 2023-09-01 00:00:00 4.85091 4.80482 1.45181 2023-09-01 00:00:00 -49.2854 15.8085 34.4068
    2023-10-01 00:00:00 2023-10-01 00:00:00 15.2679 11.3361 2.08076 2023-10-01 00:00:00 77.0512 97.8758 15.6185

    Let me know if this meets your requirements!