我正在使用的数据集是:https://www.kaggle.com/rohanrao/nifty50-stock-market-data
其中包含从2000年到2020年所有NIFTY50公司的股票市场数据。每个文件包含以下列:['Date', 'Symbol', 'Series', 'Prev Close', 'Open', 'High', 'Low', 'Last', 'Close', 'VWAP', 'Volume', 'Turnover', 'Trades', 'Deliverable Volume', '%Deliverble']
我需要将所有文件的'Close'
列编译为一个数据帧。使用Date作为索引,使用列名作为文件名,即
Date ADANIPORTS ASIANPAINTS AXISBANK .....
2000-01-01 0 1500 300
2000-02-02 1 1600 400
...
某些文件仅具有以后的数据(例如2007年1月1日),如果缺少'Close'
的值,则应将其列为0,即,直到数据可用的日期为0。] >
目前,我正在使用此代码。
df=pd.DataFrame() for filename in filenames: file=dir+filename+'.csv' data = pd.read_csv(file,usecols=lambda x: x in ['Date', 'Close']) data.rename(columns = {'Close':filename}, inplace = True) data.set_index('Date',inplace=True) df.join(data, how='outer')
这将返回(0,0)DataFrame-> df
其他我尝试过
#Initialising df with GRASIM.csv, and then using join for the other dataframes file01 = dir + "GRASIM" + '.csv' df=pd.read_csv(file01,usecols=lambda x: x in ['Date', 'Close']) df.rename(columns = {'Close':"GRASIM"}, inplace = True) df.set_index('Date',inplace = True) for filename in filenames: file=dir+filename+'.csv' data = pd.read_csv(file,usecols=lambda x: x in ['Date', 'Close']) data.rename(columns = {'Close':filename}, inplace = True) data.set_index('Date',inplace=True) df.join(data, how='outer')
但是这将返回初始初始化的数据帧,即
GRASIM Date 2000-01-03 438.30 2000-01-04 437.15 ... ...
未添加其他列。
这似乎是什么问题?
我使用的数据集是:https://www.kaggle.com/rohanrao/nifty50-stock-market-data它包含自2000年到2020年所有NIFTY50公司的股票市场数据。每个文件都包含以下内容。 ..
一种解决方法是在Python中使用zipfile模块:
我不清楚您要寻找什么输出。无论如何,我会解释我的所作所为。首先,我将文件解压缩到Kaggle
上的C-drive
文件夹中,然后使用os.chdir()
将其更改为当前目录。然后,我创建了一个空白列表,稍后我们将在其中添加数据框以循环并连接数据。