【问题标题】:Code returns empty dataframe without index代码返回没有索引的空数据框
【发布时间】:2018-07-15 02:13:28
【问题描述】:

我正在尝试使用每日股票数量的历史数据制作一个数据框,这些数据随着 Nifty 50 指数各自的交易量而上涨和下跌。 作为 python 新手,我在处理 pandas 数据帧和条件时遇到了麻烦。

下面是我写的代码,但是有很多问题:

  1. df.index = data.index 错误:ValueError:长度不匹配:预期轴有 0 个元素,新值有 248 个元素

  2. 如果我注释掉上面设置空数据帧索引的行,代码运行并在最后给出一个空数据帧。

    #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]
    
    df = pd.DataFrame(columns = {'Advances','Declines','Adv_Volume','Dec_Volume'})
    
    
    for x in nifty50_symbols:
        data = nsepy.get_history(symbol = x, start=start_date, end=end_date)
        sclose = data['Close']
        sopen = data['Open']
        svol = data['Volume']
    
    
     ##    df.index = data.index
    
     ##   for i in df.index: --- since df.index was commented out it's value was nill
        for i in data.index:
            if sclose > sopen:
                df['Advances'] = df['Advances'] + 1
                df['Adv_Volume'] = df['Adv_Volume'] + svol
    
            elif sopen > sclose:
                df['Declines'] = df['Declines'] + 1
                df['Dec_Volume'] = df['Dec_Volume'] + svol
    
    
    print(df.tail())
    

输出:

Empty DataFrame
Columns: [Dec_Volume, Declines, Advances, Adv_Volume]
Index: []

编辑:找到了代码给出空数据框的原因,因为 df.index 为空,因此从未触发过 if 语句。当我将该部分更改为 data.index 时,如果触发了语句。但是现在我不知道如何使用 IF 语句,因为它给出了错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

EDIT2:在 Akshay Nevrekar 的帮助下更新了代码:最后仍然得到一个空数据框。此外,我必须将 DF 的索引设置为 data.index 中的日期,以便以后可以将 Advances/declines 与它们各自的日期联系起来。

#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]

df = pd.DataFrame(columns = {'Advances','Declines','Adv_Volume','Dec_Volume'})


for x in nifty50_symbols:
    data = ns.get_history(symbol = x, start=start_date, end=end_date)
##    sclose = data['Close']
##    sopen = data['Open']
##    svol = data['Volume']

##    df.index = data.index

    for i in data.index:
        sclose=data.loc[i]['Close']
        sopen=data.loc[i]['Open']
        svol = data.loc[i]['Volume']

        if sclose > sopen :
            df['Advances'] = df['Advances'] + 1
            df['Adv_Volume'] = df['Adv_Volume'] + svol

        elif sopen > sclose :
            df['Declines'] = df['Declines'] + 1
            df['Dec_Volume'] = df['Dec_Volume'] + svol



print(df

)

【问题讨论】:

  • 在你的代码中添加ns.get_history()的实现。
  • 对不起,我忘了修复这里的 ns 是 nsepy。我将其导入为 ns,我将对其进行编辑和修复。
  • 你能用你最近的尝试编辑代码吗?
  • 是的,我已经这样做了。

标签: python python-3.x pandas


【解决方案1】:

您正在创建一个空数据框。

df = pd.DataFrame(columns = {'Advances','Declines','Adv_Volume','Dec_Volume'})

所以df.index 将是空的并且

 for i in df.index:
    if sclose > sopen:
        df['Advances'] = df['Advances'] + 1
        df['Adv_Volume'] = df['Adv_Volume'] + svol

    elif sopen > sclose:
        df['Declines'] = df['Declines'] + 1
        df['Dec_Volume'] = df['Dec_Volume'] + svol

你上面的for循环永远不会运行。这就是你得到一个空数据框的原因。

编辑:编辑后,我建议您在 for 循环中声明 sclose 和 sopen 变量。您正在将整列分配给变量而不是单个值。

for i in data.index:
    sclose=data.iloc[i]['Close']
    sopen=data.iloc[i]['Open']
    svol=data.iloc[i]['Volume']

    if sclose > sopen:
       df.loc[i]['Advances'] = df.loc[i]['Advances'] + 1
       df.loc[i]['Adv_Volume'] = df.loc[i]['Adv_Volume'] + svol
    elif sopen > sclose:
       df.loc[i]['Declines'] = df.loc[i]['Declines'] + 1
       df.loc[i]['Dec_Volume'] = df.loc[i]['Dec_Volume'] + svol

【讨论】:

  • 谢谢,我需要为剩余的错误提出一个新问题吗?由于此答案满足标题,但我仍然无法获得所需的输出。
  • .iloc of pandas 给了我这个错误 - TypeError: cannot do position indexing on with these indexers [2017-02-06] of
  • 然后使用loc 而不是iloc。我以为您将整数作为索引。
  • 好吧,代码运行了,但是当我最后打印 df 时仍然得到空数据帧,我仍然不知道如何将 DF 的索引设置为与 data.index 相同,所以我可以关联 Advances /使用日期拒绝数据。
  • 在分配值的地方再次使用locdf.loc[i][column]= .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
相关资源
最近更新 更多