程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How pandas removes and filters certain values or rows in a dataset

編輯:Python

Abstract
In the process of data analysis and cleaning , We may often need to remove some outliers from the data set . say concretely , Take a look at the following example .


0. Import the package we need to use
import pandas as pd
pandas Is a very common data analysis , Data processing package .anaconda I already have this bag , Pure version python You can pip install .


1. Remove some specific values

Data sets df in , For the properties appPlatform( The last column ), We want to delete the value of 2 Those samples .
How to do ? It's simple .
import pandas as pd
df[(True-df['appPlatform'].isin([2]))]

Of course , Sometimes we need to remove more than one value , At this time, you just need to isin([]) Add... To the list of . More specifically , for example , about appID This attribute , We want to get rid of it appID=278 and appID=382 The sample of .
df[(True-df['appID'].isin([278,382]))]


in addition , Sometimes we don't just think about a particular column , There are several other columns to consider . for example , We need to filter it out appPlatform=2 and appID=278 and appID=382 What about the samples ? It's simple .
df[(True-df['appID'].isin([278,382]))&(True-df['appPlatform'].isin([2]))]
. Actually , Here we see , It consists of two parts , The first part is appID Medium is equal to 278 and 382 Of , The other part is appPlatform Medium is equal to 2 Of . The two are logically related And (&)

2. Filter out a range of values
Above, we learned how to remove a specific value , below , Let's see how to filter out a range of values .
For datasets df, We want to filter out creativeID( First column ) in ID Greater than 10000 The sample of .
df[df['creativeID']<=10000]


in addition , If you want to consider multiple columns , It's the same as above , Make the two cases logical and (&) Can , But here's the thing , Use parentheses for each condition () Cover up .

original text :https://blog.csdn.net/qq_22238533/article/details/76127966 
 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved