Python 如何将一个系列转换为新的dataframe列名,并为每个列名追加历史价格?

Python 如何将一个系列转换为新的dataframe列名,并为每个列名追加历史价格?,python,pandas,data-analysis,Python,Pandas,Data Analysis,首先,我通过以下方式获得加密货币名称: tickers=pd.DataFrame(tickers) coin=tickers['symbol'].head(10) coin 然后,我定义了一个函数,可以从上面的代码中获得每个硬币名称的历史价格: def get_price(name): df=CmcScraper(name,"01-01-2019","05-10-2019") df=df.get_dataframe().set_index("Date") return

首先,我通过以下方式获得加密货币名称:

tickers=pd.DataFrame(tickers)
coin=tickers['symbol'].head(10)
coin
然后,我定义了一个函数,可以从上面的代码中获得每个硬币名称的历史价格:

def get_price(name):
    df=CmcScraper(name,"01-01-2019","05-10-2019")
    df=df.get_dataframe().set_index("Date")
    return df
因此,我如何获得一个数据框,其中硬币名称作为列名,历史收盘价作为每个硬币名称的系列。新数据框的索引是date? 如果有任何答案,我将不胜感激

如果我尝试以下代码,它会给我索引器:列表索引超出范围:

df=pd.concat([get_price(key)["Close"].rename(key) for key in coin],axis=1)
使用:

在这种情况下,索引将是时间

如果并非所有数据帧的索引都相同,则可能会生成错误。在这种情况下,您可以使用:

pd.concat([get_price(key)[Close].rename(key).reset_index() for key in coin],axis=1,join='outer')
如果您不介意丢失有关日期的信息:


不知道为什么,当我键入此代码时,它仍然会给我索引错误:列出索引超出范围>>>df=pd.concat[get_pricekey[Close].renamekey for key in coin],axis=1数据帧可能有不同的索引。或者它们的长度不一样。尽管可能性不大。你试过我答案中的另外两个代码了吗?试着忽略\u index=True错误发生在get\u price函数还是out函数中?我试着忽略\u index=True。但它给了我同样的错误,我减少了硬币,只包括3枚索引相同的硬币。但同样的错误
pd.concat([get_price(key)[Close].rename(key).reset_index() for key in coin],axis=1,join='outer')
pd.concat([get_price(key)[Close].rename(key) for key in coin],ignore_index=True,axis=1,join='outer')