This article mainly introduces pandas in python. the DataFrame method for excluding specific rows provides detailed sample code. I believe it has some reference value for everyone's understanding and learning. let's take a look at it. This article mainly introduces pandas in python. the DataFrame method for excluding specific rows provides detailed sample code. I believe it has some reference value for everyone's understanding and learning. let's take a look at it.
Preface
When you use Python for data analysis, a frequently used data structure is pandas DataFrame. for details about the basic operations of pandas. DataFrame in python, refer to this article.
Exclude specific rows from pandas. DataFrame
You can useisin()
Method to pass in the value of the required row as a list, you can also pass in the dictionary, specify the column for filtering.
However, if we only want all content that does not contain the content of a specific row, there is noisnotin()
Method. I encountered this kind of requirement in my work today. I often find that I can only use it in another way.isin()
To meet this requirement.
Example:
In [3]: df = pd.DataFrame([['GD', 'GX', 'FJ'], ['SD', 'SX', 'BJ'], ['HN', 'HB' ...: , 'AH'], ['HEN', 'HEN', 'HLJ'], ['SH', 'TJ', 'CQ']], columns=['p1', 'p2 ...: ', 'p3'])In [4]: dfOut[4]: p1 p2 p30 GD GX FJ1 SD SX BJ2 HN HB AH3 HEN HEN HLJ4 SH TJ CQ
If you only want two rows whose p1 is GD and HN, you can do this:
In [8]: df[df.p1.isin(['GD', 'HN'])]Out[8]: p1 p2 p30 GD GX FJ2 HN HB AH
However, if we want data except the two rows, we need to bypass the point.
The principle is to first extract p1 and convert it to a list, then remove unnecessary rows (values) from the list, and then useisin()
In [9]: ex_list = list(df.p1)In [10]: ex_list.remove('GD')In [11]: ex_list.remove('HN')In [12]: ex_listOut[12]: ['SD', 'HEN', 'SH']In [13]: df[df.p1.isin(ex_list)]Out[13]: p1 p2 p31 SD SX BJ3 HEN HEN HLJ4 SH TJ CQ
Summary