我需要替换数据帧中低于NaN的某个值的值。
例如,假设我需要替换NaN
的所有高于100的值df = pd.DataFrame({'a':[1,250,480],
'b':[60,51,101],
'c':[15,689,1]})
会变成:
({'a':[1,NaN,NaN],
'b':[60,51,NaN],
'c':[15,NaN,1]})
这应该是最好的方法吗?
答案 0 :(得分:2)
使用:
df = df.mask(df > 100)
df = df.where(df <= 100)
df = pd.DataFrame(np.where(df > 100, np.nan, df), index=df.index, columns=df.columns)
print (df)
a b c
0 1.0 60.0 15.0
1 NaN 51.0 NaN
2 NaN NaN 1.0
快速比较(取决于数据):
df = pd.concat([df] * 10000, ignore_index=True)
In [104]: %timeit pd.DataFrame(np.where(df > 100, np.nan, df), index=df.index, columns=df.columns)
The slowest run took 4.37 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 683 µs per loop
In [105]: %timeit df[:] = np.where(df.values <= 100, df.values, np.nan)
__main__:257: RuntimeWarning: invalid value encountered in less_equal
The slowest run took 17.24 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 957 µs per loop
In [106]: %timeit df.mask(df > 100)
1000 loops, best of 3: 1.56 ms per loop
In [107]: %timeit df.where(df <= 100)
The slowest run took 8.01 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 1.84 ms per loop
In [108]: %timeit df[df<100]
The slowest run took 5.57 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 1.89 ms per loop
答案 1 :(得分:2)
np.where
进行就地更新;
df[:] = np.where(df.values <= 100, df.values, np.nan)
df
a b c
0 1.0 60.0 15.0
1 NaN 51.0 NaN
2 NaN NaN 1.0
答案 2 :(得分:1)
最短的一个是
df[df<100]