Search code examples
pythonpandasdataframenandrop

How to drop NaN from one column according to another columns specific value


Can't figure out how to drop NaN values from specific column according to another column specific value. Part of DataFrame(df):

            vol.            group
1186      10,448,898          1
1187      nan                 0
1188      35,047,520          1
          ...   
8329      130,703             0
8330      241,489             1
8332      nan                 1
8333      101,142             0
8334      nan                 1

I need to drop nan values from vol. but only when according value in group is 1.

I tried:

df.loc[df['group'] == 1,'vol.'].dropna(inplace=True)

But df still have all values as dropna takes no effect.


Solution

  • You can change logic - select all values without 1 with nans in boolean indexing:

    #if necessary convert strings nan to missing values `NaN`s
    df['vol.'] = df['vol.'].replace('nan', np.nan)
    
    
    df = df[(df['group'] != 1) | df['vol.'].notna()]
    print (df)
                vol.  group
    1186  10,448,898      1
    1187         NaN      0
    1188  35,047,520      1
    8329     130,703      0
    8330     241,489      1
    8333     101,142      0