Pandas DataFrame:使用列轴索引添加具有行值总和的列?
Pandas DataFrame: Add Column with Sum of Row Values using Column Axis indices?
查看之前提出的问题,我找不到有用的答案,因为我的专栏是通过混合使用 pytrends 和 yfinance 值生成的。
这是获取相关数据帧的代码:
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()
我的search_rank(第一行)的returns一个pandas数据框:
date | TER | Teradyne, Inc. | Teradyne | isPartial
2004-01-01 00:00:00 | 25 | 0 | 1 | False
我想做的是删除 isPartial 列并将其替换为“Rank”列,该列将从第 1、2 和 3 列中获取值并将它们相加,这样它看起来像这样:
date | TER | Teradyne, Inc. | Teradyne | Rank
2004-01-01 00:00:00 | 25 | 0 | 1 | 26
任何关于如何实现这一目标的想法都将是一个巨大的帮助!
PS:我不想使用实际列名的原因是因为此信息会根据股票行情而改变。另外,我是 python 的极度菜鸟,基本上还在学习 >.<
删除一列
del search_rank['isPartial']
添加计算列
search_rank['Rank'] = df.apply(lambda row: row[0]+row[1] + row[2], axis=1)
我用上面的修改测试了你的代码
这是完整的代码
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)
输出:
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
查看之前提出的问题,我找不到有用的答案,因为我的专栏是通过混合使用 pytrends 和 yfinance 值生成的。
这是获取相关数据帧的代码:
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()
我的search_rank(第一行)的returns一个pandas数据框:
date | TER | Teradyne, Inc. | Teradyne | isPartial
2004-01-01 00:00:00 | 25 | 0 | 1 | False
我想做的是删除 isPartial 列并将其替换为“Rank”列,该列将从第 1、2 和 3 列中获取值并将它们相加,这样它看起来像这样:
date | TER | Teradyne, Inc. | Teradyne | Rank
2004-01-01 00:00:00 | 25 | 0 | 1 | 26
任何关于如何实现这一目标的想法都将是一个巨大的帮助!
PS:我不想使用实际列名的原因是因为此信息会根据股票行情而改变。另外,我是 python 的极度菜鸟,基本上还在学习 >.<
删除一列
del search_rank['isPartial']
添加计算列
search_rank['Rank'] = df.apply(lambda row: row[0]+row[1] + row[2], axis=1)
我用上面的修改测试了你的代码 这是完整的代码
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)
输出:
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