如何从熊猫数据提取中删除前缀

时间:2019-08-28 15:49:11

标签: python-3.x

尝试创建关联表,但希望删除所有变量的前缀“ adj_close”。

我尝试过:

corr_df.head().reset_index()后跟 del corr_df.index.name

这似乎不起作用。这是完整的代码,除了 API_KEY:

import quandl
import pandas as pd
import numpy as np

#1. Get adjusted closing prices 
quandl.ApiConfig.api_key = "API KEY"
selected = ['AMZN', 'AAPL', 'NOC', 'GE', 'TSLA']
data = quandl.get_table('WIKI/PRICES', ticker = selected,
qopts = { 'columns': ['date', 'ticker', 'adj_close'] },
date = { 'gte': '2014-1-1', 'lte': '2018-12-31' }, paginate=True)
data.head()

#2. Sort adjusted closing prices by tickers
#   Set date as index with
#       columns of tickers and their corresponding adjusted prices
clean = data.set_index('date')
table = clean.pivot(columns='ticker')
table.head()

#3. Check if assets are correlated 
#   Use pivot to rearrange symbols into columns:
df_pivot = data.pivot('date','ticker').reset_index()
df_pivot.head()
corr_df = df_pivot.corr(method='pearson')
corr_df.head(10)
#   Reset symbol as index 
corr_df.head().reset_index()
del corr_df.index.name  # will not delete the prefix 'adj_close' ???
corr_df.head(10)

但“ adj_close”显示在以下列表结果中:

                 adj_close                                        
ticker                AAPL      AMZN        GE       NOC      TSLA
          ticker                                                  
adj_close AAPL    1.000000  0.828788 -0.333861  0.863279  0.813706
          AMZN    0.828788  1.000000 -0.186319  0.974629  0.687924
          GE     -0.333861 -0.186319  1.000000 -0.135942 -0.349703
          NOC     0.863279  0.974629 -0.135942  1.000000  0.682713
          TSLA    0.813706  0.687924 -0.349703  0.682713  1.000000

0 个答案:

没有答案