【问题标题】:Iterating over a list of DataFrame objects with a for loop使用 for 循环遍历 DataFrame 对象列表
【发布时间】:2019-12-11 10:10:41
【问题描述】:

我有这个错误:

TypeError: 列表索引必须是整数或切片,而不是 DataFrame

divisasIndicaDataFrame 对象的列表,我有这个代码:

datachart=[]
def dchart ():
    for i in divisasIndica[:]:
        df=divisasIndica[i]
        data = [ dict(
            type = 'candlestick',
            open = df.Open,
            high = df.High,
            low = df.Low,
            close = df.Close,
            x = df.index,
            rsi=df.RSI,
            SMA50=df.SMA50,
            SMA200=df.SMA200,
            SAR=df.SAR,
            yaxis = 'y2',
            name = tit,
            increasing = dict( line = dict( color = INCREASING_COLOR ) ),
            decreasing = dict( line = dict( color = DECREASING_COLOR ) ),
        ) ]
        layout=dict()

        fig = dict( data=data, layout=layout )
        datachart.append(fig)

问题是我不能用for 读取DataFrames 列表吗?

【问题讨论】:

  • 你的问题是什么?
  • 你希望这条线上会发生什么:df=i.divisasIndica[i]?
  • 当您执行for i in divisasIndica[:] 时,i 将是您感兴趣的DataFrame。您不需要df=divisasIndica[i]。只需执行for df in divisasIndica[:]: 并跳过将某些内容分配给df 的部分。
  • 我正在更新我的问题...你能帮帮我吗?

标签: python dataframe


【解决方案1】:

列表的值已经是数据框,无需尝试使用索引获取它们。 for i in divisasIndica 直接为您提供divisasIndica 的所有元素,而不是它们的索引。而且不用divisasIndica[:]

将您的代码更改为:

datachart=[]
def dchart ():
    for df in divisasIndica:
        data = [ dict(
            type = 'candlestick',
            open = df.Open,
            high = df.High,
            low = df.Low,
            close = df.Close,
            x = df.index,
            rsi=df.RSI,
            SMA50=df.SMA50,
            SMA200=df.SMA200,
            SAR=df.SAR,
            yaxis = 'y2',
            name = tit,
            increasing = dict( line = dict( color = INCREASING_COLOR ) ),
            decreasing = dict( line = dict( color = DECREASING_COLOR ) ),
        ) ]
        layout=dict()

        fig = dict( data=data, layout=layout )
        datachart.append(fig)

【讨论】:

  • 我有一个包含 50 个数据框的列表......我想创建另一个包含 50 个数据框的列表,但是在我的新数据框列表中添加这个字典
  • @PedroPuertas 请不要添加不必要的 cmets。在完成 dchart(divisasIndica) 之后,您需要打印列表,而不是函数 print(datachart)
【解决方案2】:
datachart=[]
def dchart (divisasIndica):
    for df in divisasIndica:
        data = [ dict(
            type = 'candlestick',
            open = df.Open,
            high = df.High,
            low = df.Low,
            close = df.Close,
            x = df.index,
            rsi=df.RSI,
            SMA50=df.SMA50,
            SMA200=df.SMA200,
            SAR=df.SAR,
            yaxis = 'y2',
            name = titu,
            increasing = dict( line = dict( color = INCREASING_COLOR ) ),
            decreasing = dict( line = dict( color = DECREASING_COLOR ) ),

        ) ]
        layout=dict()

        fig = dict( data=data, layout=layout )
        datachart.append(fig)

【讨论】:

    猜你喜欢
    • 2012-05-09
    • 2019-09-07
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多