i have the following data frame
X Y
DateTime
2019-07-22 00:00:00 0.5 0.6
2019-07-22 00:01:00 NaN 0.8
2019-07-22 00:02:00 0.7 NaN
2019-07-22 00:03:00 0.4 0.3
I want to replace all items in a row with NaN if one value in the row is NaN, maintaining the index as:
X Y
DateTime
2019-07-22 00:00:00 0.5 0.6
2019-07-22 00:01:00 NaN NaN
2019-07-22 00:02:00 NaN NaN
2019-07-22 00:03:00 0.4 0.3
How do i do this?
Use DataFrame.mask
with boolean mask:
df1 = df.mask(df.isna().any(axis=1))
print (df1)
X Y
DateTime
2019-07-22 00:00:00 0.5 0.6
2019-07-22 00:01:00 NaN NaN
2019-07-22 00:02:00 NaN NaN
2019-07-22 00:03:00 0.4 0.3
Details :
Test missing values by DataFrame.isna
:
print (df.isna())
X Y
DateTime
2019-07-22 00:00:00 False False
2019-07-22 00:01:00 True False
2019-07-22 00:02:00 False True
2019-07-22 00:03:00 False False
And then test if at least one True
per rows by DataFrame.any
:
print (df.isna().any(axis=1))
DateTime
2019-07-22 00:00:00 False
2019-07-22 00:01:00 True
2019-07-22 00:02:00 True
2019-07-22 00:03:00 False
dtype: bool
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.