Vladislav Ladenkov Vladislav Ladenkov - 1 month ago 17
Python Question

Seperate dataframe by certain value

I have:

Out[94]:
customer_id bad
0 1 0
1 4 1
2 3 0
3 2 NaN
4 7 NaN


I want to make a new dataframe
first
, which consists of all rows with
bad == NaN
and
Second
which consists of others.


  1. We need to find the first instance of
    NaN
    . I did stupid thing, but i could do it more gentle:

    for index, value in enumerate(target.bad):
    if np.isnan(value):
    break



    1. We need to reassign new dataframes(I tried this, but failed):




First = target.irow([1:index])


I've been coding python for a while and i cant belieave iam stuck here

Answer

Maybe I am missing something but couldn't you just use:

first = df[df['bad'].isnull()]
second = df[~df['bad'].isnull()]