What's new

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

Fever

Active member
Staff member
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 >.<

Continue reading...
 
Top