创建一个大熊猫Dataframe

时间:2018-02-05 15:02:59

标签: python-3.x pandas dataframe

我的代码正在检索从今天开始365天以来50种不同股票的历史数据。

我想将所有这些数据存储在一个数据框中以便于分析,在这里我想过滤所有这些数据,按日期计算并计算给定日期的前进/下降股票数量。

我的代码:

import datetime
from datetime import date, timedelta
import pandas as pd
import nsepy as ns

#setting default dates
end_date = date.today()
start_date = end_date - timedelta(365)

#Deriving the names of 50 stocks in Nifty 50 Index
nifty_50 = pd.read_html('https://en.wikipedia.org/wiki/NIFTY_50')

nifty50_symbols = nifty_50[1][1]




for x in nifty50_symbols:
    data = ns.get_history(symbol = x, start=start_date, end=end_date)
    big_df = pd.concat(data)

输出:

Traceback (most recent call last):
  File "F:\My\Getting data from NSE\advances.py", line 27, in <module>
    big_df = pd.concat(data)
  File "C:\Users\Abinash\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 212, in concat
    copy=copy)
  File "C:\Users\Abinash\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 227, in __init__
    '"{name}"'.format(name=type(objs).__name__))
TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

我是python的新手,我浏览了pandas教程并看到pandas.concat用于将多个数据帧合并为一个。我可能已经理解错了。

1 个答案:

答案 0 :(得分:1)

连接数据必须是可迭代的,例如list。

results = []
for x in nifty50_symbols:
    data = ns.get_history(symbol = x, start=start_date, end=end_date)
    results.append(data)

big_df = pd.concat(results)