-1

I have a dictionary of dataframes dico where each dataframe looks something like:

  Cust   Cont  Rate
0 Cust   Cont  Rate
1 Vent   8001  TOU-GS
2 Vent   8001  TOU-GS
3 nan    nan   nan

I am trying to operate on the dictionary to clean up each dataframe, first by dropping the row containing column headers (whichever row they happen to be in) and dropping any rows or columns full of nulls.

colheaders = ['Cust','Cont','Rate']

for key, item in dico.items():
    item = item.drop(item[item.iloc[:,0].isin(colheaders)].index)
    item = item.dropna(how = 'all')
    item = item.dropna(how = 'all', axis=1)

My code doesn't return any errors, but it doesn't show any changes. Any idea what I'm doing wrong here? Operating on dictionary of dataframes in this fashion seemed to work for this solution. Perhaps this is a larger lesson of learning how to operate on dataframes in a loop, but I just can't seem to crack it.

christina
  • 28
  • 7

1 Answers1

1

You forget to re-assign the values of your dictionnary. That's why the changes are ineffective.

Use this :

colheaders = ['Cust','Cont','Rate']

for key, item in dico.items():
    item = item.drop(item[item.iloc[:,0].isin(colheaders)].index)
    item = item.dropna(how = 'all')
    item = item.dropna(how = 'all', axis=1)
    dico[key] = item

Quick note: Use dico, my_dict, dfs_dict, ... as a name of your dictionnary variable instead of dict since this one is a python constructor.

Timeless
  • 22,580
  • 4
  • 12
  • 30
  • 1
    thank you I do not have much experience working with dictionaries so I was a little out my depth with this one but it truly is as simple as that. My dictionary name is much more unique to my specific data, I changed the name for this more simple example in my question though. – christina Oct 06 '22 at 18:59