How to select non null rows in a dataframe

Question:

Shop_name         Bikes_available             Shop_location       Average_price_of_bikes   Rating_of_shop

NYC Velo          Ninja,hbx                      Salida                   5685$               4.2               
Bike Gallery      dtr,mtg,Harley Davidson        Portland                 6022$               4.8

This dataset is stored in dataframe named df. I am tying to create new dataframe that contains only those rows whose shop name, bikes_available and shop_location values are not null

xtrain = df[df['Shop_name','Bikes_available','Shop_location']!=NULL]

Its showing keyerror: (‘Shop_name’,’Bikes_available’,’Shop_location’)

Asked By: Ashu

||

Answers:

First, select multiple columns use [[]]. Then, test for non missing values by DataFrame.notna with DataFrame.all:

xtrain = df[df[['Shop_name','Bikes_available','Shop_location']].notna().all(axis=1)]
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.