separate NaN text columns into other dataframe

Viewed 38

I have two columns X and Y which are text columns. X is my feature and Y is my target variable. My Y variable consists of NaN values. I want to separate NaN values of Y into a separate data frame so that I will use them in my test dataset.

   Y
1. aaa
2. bbb
3. ccc
4. NaN
5. NaN
6. ddd

Could anyone tell me how to separate the NaN rows?

1 Answers

I believe need boolean indexing with isna:

mask = df['Y'].isna()
#oldier pandas versions
#mask = df['Y'].isnull()
#DataFrame with NaNs
df1 = df[mask]
#DataFrame with no NaNs
df2 = df[~mask]
Related