pandas python panda中的append和concat后缺少列

vsdwdz23  于 2023-03-06  发布在  Python
关注(0)|答案(1)|浏览(231)

尝试下载历史股票价格的股票脚本和concat文件束。问题是最终输出没有给我的'日期'列,这是第一列
下面是我的代码:

from nsepy import get_history
import pandas as pd
from datetime import date

#Equity details contains list of stock symbols - Example: SBIN, AARTIPHARM, KFINTECH, HDFC
equity_details = pd.read_csv('C:/Users/am364971/Downloads/EQUITY_SAMPLE.csv')
equity_details.rename(columns={"SYMBOL": "Symbol"}, inplace=True)

# Create empty list df to append the output data from 'For loop' below
df = []

# So the for loop will download historical stock price details one at a time and append to the list df

for name in equity_details.Symbol:
    try:
        data = get_history(symbol=name,
                   start=date(2023,1,1),
                   end=date(2023,2,28))
        df.append(data)         
    except Exception as e:
        print(f'{name} ===> {e}')

df = pd.concat(df)

df.to_csv(f'C:/Data/Combined_StockPrices.csv')

问题是最终输出没有"日期"列。请指导我遗漏了什么

bxgwgixi

bxgwgixi1#

df中没有“Date”列,因为它被设置为索引。请尝试在连接前添加.reset_index()

data = get_history(symbol=name,start=date(2023,1,1),end=date(2023,2,28)).reset_index()

相关问题