简体   繁体   English

如果循环熊猫数据框字典

[英]If loop for a dictionary of pandas dataframes

I have a dictionary of dataframes.我有一个数据框字典。 I defined the dict as range_, such that when I need a certain dataframe, I can call it range_[i].我将 dict 定义为 range_,这样当我需要某个数据框时,我可以将其称为 range_[i]。

在此处输入图片说明

For every data frame in the dictionary, I want to add an extra column.对于字典中的每个数据框,我想添加一个额外的列。 So what I did is the following:所以我所做的是以下内容:

for i in range_selected:
    for index in range_[i].index:
        range_[i].loc[index,'label(1 = fault)'] = ''

Now, I want to add a value in every row of this new column.现在,我想在这个新列的每一行中添加一个值。 It can be 0 or 1. My condition is that if the value of 'pdis1' at the first row of every dataframe - the value of the 9-10th row is smaller than zero, then append 1 for every row of the new column, else 0. I tried with this:它可以是 0 或 1。我的条件是,如果每个数据帧第一行的 'pdis1' 值 - 第 9-10 行的值小于零,则为新列的每一行附加 1,否则 0. 我试过这个:

for r in range_:
    for index in range_[r].index:
        if range_[r]['pdis1'].iloc[0] - range_[r]['pdis1'].iloc[10]:
            range_[r].loc[index,'label(1 = fault)'] = '1' 

but I get 'IndexError: single positional indexer is out-of-bounds'但我得到“IndexError:单个位置索引器越界”

Can anyone help me out?谁能帮我吗? Thank you谢谢

That happens when you try to access an index that does not exist in the DF.当您尝试访问 DF 中不存在的索引时会发生这种情况。

If you are sure that all the data frames have at least 10 rows, then this should work.如果您确定所有数据框都至少有 10 行,那么这应该可行。 If you still get this error, it means that one of the DataFrames has less than 10 rows.如果仍然出现此错误,则表示其中一个 DataFrame 的行数少于 10。 Thus your rule for determining the faultiness is invalid.因此,您确定故障的规则无效。

for df in range_.values():
    if df['pdis1'].iloc[0] - df['pdis1'].iloc[10]:
        val = '1'
    else:
        val = '0'
    
    df['label(1 = fault)'] = val

I changed the iteration to look a bit more pythonic : )我改变了迭代看起来更像pythonic :)

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

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