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.
CodePudding user response:
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.