我有一个这样的DataFrame
gauge satellite
1979-06-23 18:00:00 6.700000 2.484378
1979-06-27 03:00:00 NaN 8.891460
1979-06-27 06:00:00 1.833333 4.053460
1979-06-27 09:00:00 NaN 2.876649
1979-07-31 18:00:00 6.066667 1.438324
我想要这样一个数据帧
gauge satellite
1979-06-23 18:00:00 6.700000 2.484378
1979-06-27 03:00:00 NaN NaN
1979-06-27 06:00:00 1.833333 4.053460
1979-06-27 09:00:00 NaN NaN
1979-07-31 18:00:00 6.066667 1.438324
答案 0 :(得分:7)
我将做什么reindex
df.dropna().reindex(df.index)
答案 1 :(得分:3)
mask
:
df.mask(df.gauge.isna())
gauge satellite
1979-06-23 18:00:00 6.700000 2.484378
1979-06-27 03:00:00 NaN NaN
1979-06-27 06:00:00 1.833333 4.053460
1979-06-27 09:00:00 NaN NaN
1979-07-31 18:00:00 6.066667 1.438324
答案 2 :(得分:2)
使用np.where
添加nan
import numpy as np
df['satellite'] = np.where(df['gauge'].isnull(),np.nan,df['satellite'])
使用.loc
和isnull
df.loc[df['guage'].isnull(),'satellite'] = np.nan
答案 3 :(得分:2)
您可以使用np.where
:
df['satellite'] = np.where(df['gauge'].isna(), np.NaN, df['satellite'])
df['gauge'] = np.where(df['satellite'].isna(), np.NaN, df['gauge'])
答案 4 :(得分:2)
您需要确定行中是否有np.nan
。 .any(1)
为您提供了连续的遮罩。
df.loc[df.isna().any(1)] = np.nan
输出:
gauge satellite
1979-06-23 18:00:00 6.700000 2.484378
1979-06-27 03:00:00 NaN NaN
1979-06-27 06:00:00 1.833333 4.053460
1979-06-27 09:00:00 NaN NaN
1979-07-31 18:00:00 6.066667 1.438324