1

I have a dictionary of 12 dfs named f and each df have the same columns: BacksGas_Flow_sccm, ContextID, StepID,Time_Elapsed, iso_forest, alarm.

What I am trying to do is to count the number of unique values in the BacksGas_Flow_sccm column and if the number id unique value is less than 10, I want to delete that df from the dictionary.

I tried doing something like this:

v = list(f.values())

for i in range (0, len(v)):
    if (v[i]['BacksGas_Flow_sccm'].nunique()) < 10:
        del (v[i])

but I get the following error:

Traceback (most recent call last):

  File "<ipython-input-8-580f2e039fa5>", line 2, in <module>
    if (v[i]['BacksGas_Flow_sccm'].nunique()) < 10:

IndexError: list index out of range

Any suggestions as to how can I do this?

P.S:

The number of unique values in the BacksGas_Flow_sccm of all the dfs

df    unique values
1          2
2          5
3         373
4          2
5          4
6          3
7          4
8         560
9         141
10         4
11         5
12         4

I want to drop all the dfs in the dictionary except the 3rd, 8th and 9th.

I know it is not a good thing to share screenshots of the dfs, but since there are multiple dfs, it is difficult to post a sample df here. but this is how my dictionary of dfs look

enter image description here

some_programmer
  • 3,268
  • 4
  • 24
  • 59
  • 3
    I think that after deleting an item in the loop, the index resets and that's why you might be getting the error, why not do it in two steps, first retrieving the indexes of the DFs that should be deleted and next deleting those index from the lists. – Celius Stingher Sep 10 '19 at 18:25
  • https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating Just create a new dictionary with the comprehension, keeping only the elements you want. – ALollz Sep 10 '19 at 18:25

1 Answers1

3

Celius Stingher is right about the reason for the error

I think that after deleting an item in the loop, the index resets and that's why you might be getting the error [....]

This should work.

dictNew = dict()
for key,value in f.items():
    if (value['BacksGas_Flow_sccm'].nunique()>=10):
        dictNew[key] = value
f = dictNew
Harsha
  • 353
  • 1
  • 15