简体   繁体   English

使用for循环遍历DataFrame对象的列表

[英]Iterating over a list of DataFrame objects with a for loop

I have this error: 我有这个错误:

TypeError: list indices must be integers or slices, not DataFrame TypeError:列表索引必须是整数或切片,而不是DataFrame

divisasIndica is a list of DataFrame objects, and I have this code: 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)

The question is can't I read a list of DataFrames with for ? 问题是我不能阅读带有for的DataFrames列表吗?

The values of your list are already dataframes, no need to try to get them using an index. 列表的值已经是数据帧,无需尝试使用索引获取它们。 for i in divisasIndica gives you all the elements of divisasIndica directly, not their indexes. for i in divisasIndica给你所有的元素divisasIndica直接,不是他们的索引。 And there is no need to do divisasIndica[:] . 并且不需要做divisasIndica[:]

Change your code to this: 将代码更改为此:

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)
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM