Search code examples
pandasdictionaryfor-loopboolean-indexing

pandas boolean indexing of dataframe in dictionary of data frames


So, this is probably a really simple problem, but I've not found a solution yet. I apologize for my stupidity. (I'm guessing my ignorance of terminology has impeded my searching here)

I have a dictionary of dataframes (showing 2 in here, but it contains more).

    df_dict={'P1': df0,'P2': df1}

Each dataframe may or may not have rows that I need to remove for further processing. I can easily remove these rows from one dataframe like so:

    df0=df_dict['P1']
    df0=df0[df0.well !='1']

But if I loop through the dictionary like this, nothing happens. The unwanted rows are still in the dataframes.

    for key,df in df_dict.items():
        df=df[df.well !='1']

I stupidly plunked inplace=True into the .loc and obviously that didn't work. Side note, I can't do df[~df.well.str.contains('1')] because this would remove rows that have correct positions like 'A01'

Again, I'm probably doing something very ignorant, and am very grateful for your help!


Solution

  • Please change the 'for loop' to as mentioned below :

    for key,df in df_dict.items():
        df_dict[key]=df[df.well !='1']
    

    This should fix the issue.