![](/img/trans.png)
[英]Set the values out of the defined set to a given value (f.e. NaN) for a column in pandas data frame
[英]Set the values out of the defined interval limits to a given value (f.e. NaN) for a column in pandas data frame
如果定义了有效值的间隔限制,则应将其中的所有pandas数据帧列值设置为给定值fe NaN
。 定义限制和数据帧内容的值可以假定为数字类型。
具有以下限制和数据框架:
min = 2
max = 7
df = pd.DataFrame({'a': [5, 1, 7, 22],'b': [12, 3 , 10, 9]})
a b
0 5 12
1 1 3
2 7 10
3 22 9
设置列a
的限制将导致:
a b
0 5 12
1 NaN 3
2 7 10
3 NaN 9
where
between
df.a=df.a.where(df.a.between(min,max),np.nan)
df
Out[146]:
a b
0 5.0 12
1 NaN 3
2 7.0 10
3 NaN 9
或clip
df.a.clip(min,max)
Out[147]:
0 5.0
1 NaN
2 7.0
3 NaN
Name: a, dtype: float64
你可以使用.loc
与between
也
import pandas as pd
import numpy as np
df = pd.DataFrame({'a': [5, 1, 7, 22],'b': [12, 3 , 10, 9]})
min = 2
max = 7
df.loc[~df.a.between(min,max), 'a'] = np.nan
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.