0

Dataset containing some null values

I want to create a new boolean column that has a 0/1 depending if the ts_booking_at is null or not.

Currently I'm using the following code:

contacts.iloc[contacts["ts_accepted_at"] is np.nan, "accept"] = False
contacts.iloc[contacts["ts_accepted_at"] is not np.nan, "accept"] = True

But the following error returns

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

3
  • A few issues. You need to use loc not iloc to select the column by label more information here. You can't use is to test if something is NaN in pandas since is is going to assess if the Series itself (contacts["ts_accepted_at"]) is np.nan not each value in the Series more details here. To check for NaN you can use isna/isnull as outlined here or the inverse notna/notnull Commented Feb 17, 2022 at 1:51
  • So contacts.loc[contacts["ts_accepted_at"].isna(), "accept"] = False and contacts.loc[contacts["ts_accepted_at"].notna(), "accept"] = True Commented Feb 17, 2022 at 1:53
  • Or even more simply by just assigning the result of the boolean index to the column: contacts['accept'] = contacts["ts_accepted_at"].notna() Commented Feb 17, 2022 at 1:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.