0

This is a the dataframe in pandas I have

dict = {'First Score':[100, 90, np.nan, 95],
        'Second Score': [30, 45, 56, np.nan],
        'Third Score':[np.nan, 40, 80, 98]}

# creating a dataframe from dictionary
df = pd.DataFrame(dict)

I am trying to eliminate rows with NA values at once. So is there a way to do this through loops For example I am trying this on 1 column like below

df_first_score = pd.notnull(df['First Score'])  ### find not null values
df[df_first_score]
   First Score  Second Score  Third Score
0        100.0          30.0          NaN
1         90.0          45.0         40.0
3         95.0           NaN         98.0

So this way I am doing for all columns manually. Is there a way to achieve this through loops? So that I get below output

final_df
   First Score  Second Score  Third Score
1         90.0          45.0         40.0

I know this can be done in other ways, But wanted to know if we can achieve this through loops

manu p
  • 952
  • 4
  • 10

1 Answers1

0

Try this:

df[~df.isnull().any(axis=1)]

The df.isnull.any(axis=1) part returns a boolean Series like so:

0     True
1    False
2     True
3     True
dtype: bool

Using the inverter (~) in combination with that boolean series, result in:

   First Score  Second Score  Third Score
1         90.0          45.0         40.0
JarroVGIT
  • 4,291
  • 1
  • 17
  • 29