简体   繁体   中英

How to fill null values in a column conditionally in pandas

I have the following dataframe :

time                    label
2020-03-03 08:35:03.585 ok
2020-03-03 08:05:01.288 ok
2020-03-03 11:50:01.944 faulty
2020-03-03 08:45:04.540 ok
2020-03-12 10:30:02.227 None
2020-03-12 11:10:02.385 None
2020-03-05 11:15:03.526 None
2020-03-10 10:55:01.084 faulty
2020-03-05 11:35:04.563 None

I would like to only fill null values in label column where time is less than 2020-03-10 .

i tried

 df[df["label"].isna()] =np.where(df['triggerTs'] < '2020-03-10', 'ok' ,'no label')

But apparently it is not the correct way to do it because returns this error

ValueError: Must have equal len keys and value when setting with an iterable

In your solution is necessary filter missing rows for both sides for same length of assigned array to label column:

m = df["label"].isna()
df.loc[m, 'label'] = np.where(df.loc[m, 'time'] < '2020-03-10', 'ok' ,'no label')
print (df)
                     time     label
0 2020-03-03 08:35:03.585        ok
1 2020-03-03 08:05:01.288        ok
2 2020-03-03 11:50:01.944    faulty
3 2020-03-03 08:45:04.540        ok
4 2020-03-12 10:30:02.227  no label
5 2020-03-12 11:10:02.385  no label
6 2020-03-05 11:15:03.526        ok
7 2020-03-10 10:55:01.084    faulty
8 2020-03-05 11:35:04.563        ok

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM