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 describes pandas in python. sample Code of the DataFrame exclusion method for specific rows. the detailed sample code is provided in this article. I believe it has some reference value for everyone's understanding and learning. let's take a look at it.
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
Related articles:
In python, pandas. DataFrame sums rows and columns and adds the new row and column sample code.
Pandas. DataFrame (create, index, add and delete) in python
The above is a detailed description of pandas. DataFrame in python to exclude the sample code of a specific line of methods. For more information, see other related articles in the first PHP community!