如何重新标记连接到现有数据框的一些新计算的列?
Posted
技术标签:
【中文标题】如何重新标记连接到现有数据框的一些新计算的列?【英文标题】:How to relabel some newly calculated columns that were concatenated to an existing dataframe? 【发布时间】:2021-06-24 04:24:34 【问题描述】:我的目标是重新标记我连接到现有数据框的一些新计算的列。
简而言之,我有以下步骤:
一个。我从雅虎财经图书馆(a 235 x 28) 乙。然后我计算了对数返回 (235 x 4) 并连接 他们 c。创建了新标签 d.如何新替换 b 的标签 添加了列?我的问题是如何执行步骤 d? (见下面的代码)
# Install yfinance and matplotlib package
!pip install yfinance
!pip install matplotlib
# Import yfinance and matplotlib
import yfinance as yf
import matplotlib.pyplot as plt
# list of interesting stocks, ETFs by ticker
slist = ['SPY', 'AAPL', 'QQQ', 'AMZN']
# step a: Get the data for list of stocks specified by s plus start date, and end date
df = yf.download(slist,'2020-01-01','2020-12-06')
print(df)
print(df.shape)
# step b: concatenate the Log Returns onto the existing df
df = pd.concat([df, np.log(df['Adj Close']/df['Adj Close'].shift(1)).dropna()], axis = 1)
print(df)
print(df.shape)
# one can see that the concatenated columns don't have the appropriate header labels yet
# they just have the name of the tickers in slist
df.head().iloc[ :, -len(slist):]
# step c: create new labels where it becomes ticker + ' Log Rtn' and try to use these
slabel = [s + ' Log Rtn' for s in slist]
print(slabel)
# step d: Here I would like to rename the headers of the part that was added to dataframe, df, such that it uses (slabel)
# show correct end results hopefully
df.head().iloc[ :, -len(slist):]
【问题讨论】:
【参考方案1】:如果新列标题的名称来自slist
;那么这应该可以工作。
slist = ['SPY', 'AAPL', 'QQQ', 'AMZN']
for prev_hd, new_hd in zip(slist, slabel):
df = df.rename(columns=prev_hd: new_hd)
更一般的
df = df.rename(columns='oldName1': 'newName1', 'oldName2': 'newName2')
【讨论】:
以上是关于如何重新标记连接到现有数据框的一些新计算的列?的主要内容,如果未能解决你的问题,请参考以下文章
如何将 numpy 数组存储在 Pandas 数据框的列中?
如何将 numpy 数组存储在 Pandas 数据框的列中?