Python 如何选择指定范围内的dataframe列值?
这是我的代码:Python 如何选择指定范围内的dataframe列值?,python,pandas,numpy,matplotlib,Python,Pandas,Numpy,Matplotlib,这是我的代码: df = pd.read_csv("/content/Intel_AI4Y/My Drive/Intel_AI4Y_Colab/Module_16/data/Students_Score1.csv") names = ["Student No." ,"Hours spent studying in a day", "Mathematics score", "English score&qu
df = pd.read_csv("/content/Intel_AI4Y/My Drive/Intel_AI4Y_Colab/Module_16/data/Students_Score1.csv")
names = ["Student No." ,"Hours spent studying in a day", "Mathematics score", "English score","Science score"]
df.columns = names
Mathematics_score = df.iloc[:, 0]
df = df[~df.iloc[:, 0].between(100, 0, inclusive=False)]
print(df.describe())
print (df.info())
我试图从数学分数中删除错误数据,该值低于0或高于100。我不确定我该如何编写这个。有人能帮忙吗?因为您的数据框带有标题。我真的建议使用如下的遮罩过滤器
df = df[(df['Mathematics score'] > 0) & (df['Mathematics score'] < 100)]
df=df[~df.iloc[:,0]。介于100,0之间,包括=False]几乎是正确的
需要左边界和右边界,分别为0和100。
~实际上并非如此。df.iloc[:,0]。between0,100,inclusive=False返回0到100之间的所有值,但是~df.iloc[:,0]。between0,100,inclusive=False返回值=100。
要返回介于0和100之间的值,请使用df[df.iloc[:,0]。介于0和100之间,包括=False]
也看到
有关.iloc的正确用法,请参阅。iloc[:,0]表示您已经选择了所有行::和索引0处的列。我的示例数据只有一列,因此索引为0。您需要验证感兴趣的列的索引。
作为pd进口熊猫
将numpy作为np导入
示例数据帧
np.random.seed100
df=pd.DataFrame{'values':[np.random.randint-100200表示范围500内的}
值介于0和100之间
df[df.iloc[:,0]。介于0,100之间,包括=False]
价值观
43
37
55
41
35
数值=100
df[~df.iloc[:,0]。介于0和100之间,包括=False]
价值观
-92
180
-21
-47
-34
df[~df.iloc[:, 2].between(0, 100, inclusive=False)]