Vladislav Ladenkov Vladislav Ladenkov - 11 months ago 76
Python Question

Seperate dataframe by certain value

I have:

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
, which consists of all rows with
bad == NaN
which consists of others.

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

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

    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 Source

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

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