Search code examples
pythonpandasdataframeyfinance

Pandas DataFrame: Add Column with Sum of Row Values using Column Axis indices?


Looking through previously asked questions, I couldn't find the answer that helped, since my columns are generated by using a mix of both pytrends and yfinance values.

Here is the code to get the dataframe in question:

import yfinance as yf
from pytrends.request import TrendReq as tr

ticker = "TER"
pytrends = tr(hl='en-US', tz=360)

# =============================================================================
# Get Stock Information
# These variables are stored as DataFrames
# =============================================================================
stock = yf.Ticker(ticker)
i = stock.info
stock_info = {'Ticker':ticker}
stock_info.update(i)

# =============================================================================
# Get Google Trends Ranking for our Stock
# =============================================================================
longName = stock_info.get('longName')
shortName = stock_info.get('shortName').split(',')[0]

keywords = [ticker, longName, shortName]
pytrends.build_payload(keywords, timeframe='all')
search_rank = pytrends.interest_over_time()

This returns a pandas dataframe for my search_rank (the first row):

date                | TER | Teradyne, Inc. | Teradyne | isPartial
2004-01-01 00:00:00 | 25  | 0              | 1        | False

What I would like to do is to drop the isPartial column and replace it with a "Rank" column, which would take the values from columns 1, 2 and 3 and add them together so it would instead look like this:

date                | TER | Teradyne, Inc. | Teradyne | Rank
2004-01-01 00:00:00 | 25  | 0              | 1        | 26

Any ideas on how I would get this accomplished would be a HUGE help!

PS: The reason I don't want to use the actual column names is because this info will change depending on the ticker. Also, I'm an extreme noob at python, and basically still learning >.<


Solution

  • Drop a column

    del search_rank['isPartial']
    

    Add a calculated column

    search_rank['Rank'] = df.apply(lambda row: row[0]+row[1] + row[2], axis=1)
    

    I tested your code with above modification here is the full code

    import yfinance as yf
    from pytrends.request import TrendReq as tr
    
    ticker = "TER"
    pytrends = tr(hl='en-US', tz=360)
    
    # =============================================================================
    # Get Stock Information
    # These variables are stored as DataFrames
    # =============================================================================
    stock = yf.Ticker(ticker)
    i = stock.info
    stock_info = {'Ticker':ticker}
    stock_info.update(i)
    
    # =============================================================================
    # Get Google Trends Ranking for our Stock
    # =============================================================================
    longName = stock_info.get('longName')
    shortName = stock_info.get('shortName').split(',')[0]
    
    keywords = [ticker, longName, shortName]
    pytrends.build_payload(keywords, timeframe='all')
    search_rank = pytrends.interest_over_time()
    del search_rank['isPartial']
    search_rank['Rank'] = search_rank.apply(lambda row: row[0]+row[1]+row[2] , axis=1)
    
    print(search_rank)
    

    OUTPUT:

     Date        TER  Teradyne, Inc.  Teradyne  Rank
    2004-01-01   25               0         1    26
    2004-02-01   25               0         1    26
    2004-03-01   29               0         1    30