Python 删除未包含在其他两列之间的间隔中的数据帧RAW

Python 删除未包含在其他两列之间的间隔中的数据帧RAW,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,我需要删除不包含在区间中的观察值(其限值包含在其他两列中),并用平均值或中位数替换NaN值。我认为我应该使用带有三个条件的if,但我对数据帧不太有信心 数据帧示例: col1 lower_bound upper_bound 3 2 6 1 2 6 3 2 6 5 2 6 8 2 6

我需要删除不包含在区间中的观察值(其限值包含在其他两列中),并用平均值或中位数替换NaN值。我认为我应该使用带有三个条件的if,但我对数据帧不太有信心

数据帧示例:

col1  lower_bound  upper_bound
  3        2            6 
  1        2            6 
  3        2            6 
  5        2            6 
  8        2            6 
  4        2            6 
 NaN       2            6 
所需输出示例:

 col1  lower_bound  upper_bound
  3        2            6 
  3        2            6 
  5        2            6 
  4        2            6 
mean/mdn   2            6 

提前感谢您的帮助

您可以通过两个步骤完成此操作:用平均值或中位数填充NaN,并使用or 2条件进行索引,以获得
col1
在边界之间的行

# Fill NaN in col1 with the mean
df.col1.fillna(df.col1.mean(),inplace=True)
# or with the median 
# df.col1.fillna(df.col1.median(),inplace=True)

# Index based on your conditions:
df[df.col1.between(df.lower_bound, df.upper_bound)]
# or:
#df[(df.col1 > df.lower_bound) & (df.col1 < df.upper_bound)]

   col1  lower_bound  upper_bound
0   3.0            2            6
2   3.0            2            6
3   5.0            2            6
5   4.0            2            6
6   4.0            2            6
#在col1中填入平均值
df.col1.fillna(df.col1.mean(),inplace=True)
#还是中位数
#df.col1.fillna(df.col1.median(),inplace=True)
#根据您的情况编制索引:
df[df.col1.between(df.lower_bound,df.upper_bound)]
#或:
#df[(df.col1>df.lower_bound)和(df.col1
您可以通过两个步骤完成此操作:用平均值或中位数填充NaN,并使用or 2条件建立索引,以获取
col1
位于边界之间的行

# Fill NaN in col1 with the mean
df.col1.fillna(df.col1.mean(),inplace=True)
# or with the median 
# df.col1.fillna(df.col1.median(),inplace=True)

# Index based on your conditions:
df[df.col1.between(df.lower_bound, df.upper_bound)]
# or:
#df[(df.col1 > df.lower_bound) & (df.col1 < df.upper_bound)]

   col1  lower_bound  upper_bound
0   3.0            2            6
2   3.0            2            6
3   5.0            2            6
5   4.0            2            6
6   4.0            2            6
#在col1中填入平均值
df.col1.fillna(df.col1.mean(),inplace=True)
#还是中位数
#df.col1.fillna(df.col1.median(),inplace=True)
#根据您的情况编制索引:
df[df.col1.between(df.lower_bound,df.upper_bound)]
#或:
#df[(df.col1>df.lower_bound)和(df.col1
@sacul它返回给我这个错误:AttributeError:“function”对象没有属性“col1”您是否碰巧定义了一个与您的数据帧同名的函数?不,我更改了它
类型(df)
的输出是什么?嗯,这没有多大意义。如何创建一个包含
上限
下限
col1
列的系列?用
NaN
替换0不应将数据帧的类型更改为系列。一定还有别的事upstream@sacul它返回给我这个错误:AttributeError:“function”对象没有属性“col1”您是否碰巧定义了一个与数据帧同名的函数?不,我更改了它
type(df)
?hmm的输出没有多大意义。如何创建一个包含
上限
下限
col1
列的系列?用
NaN
替换0不应将数据帧的类型更改为系列。上游一定还有别的事情