I can remove all rows with nan in a column with this line:
df2 = df.dropna(subset=['columnA'])
How do I remove all rows that have values other than NaN?
To drop all the rows which contain only missing values, pass the value 0 to the axis parameter and set the value how='all' .
By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .
Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).
You can remove missing values ( NaN ) from pandas. DataFrame , Series with dropna() . This article describes the following contents. If you want to extract rows and columns with missing values, see the following article.
You can do drop
df2 = df.dropna(subset=['columnA'])
df1 = df.drop(df2.index)
df.loc[lambda x:x.columnA.isnull()]
I might be missing something in the question.
Just keep the rows where value is equal to np.nan
As @rafaelc pointed out np.nan == np.nan
is false
.
And I was completely wrong I am leaving the answer here just to keep the comments here for anyone who comes looking.
Changing based on that.
df2 = df[df['ColumnA'] != np.nan] # WRONG ANSWER
df1 = df[~(df['ColumnA'] != np.nan)] #WRONG ANSWER
# perform function on df1 # WRONG ANSWER
df_f = pd.concat([df1,df2])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With