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’)
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)]
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’)
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)]