This article mainly gives you a detailed explanation of python in pandas. Dataframe exclude specific Line Method sample code, the text gives the detailed sample code, I believe that everyone's understanding and learning has a certain reference value, the need for friends to see together below.
Pandas. Dataframe Exclude specific lines
If we want a filter like Excel, as long as one or more of the rows, you can use the method to pass the values of the isin()
required rows in a list, and you can pass in the dictionary and specify the columns to filter.
But if we only want content that does not contain a specific line in everything, there is no isnotin()
way. I met this demand today, and often find it in a different way isin()
to achieve this requirement.
Examples are as follows:
In [3]: df = PD. DataFrame ([' GD ', ' GX ', ' FJ '], [' SD ', ' SX ', ' BJ '], [' HN ', ' HB ' ...:, ' AH '], [' HEN ', ' HEN ', ' HLJ '], [' SH ', ' TJ ', ' CQ '], C olumns=[' 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 want to P1 only two lines for 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
But if we want data beyond these two lines, we need to get around the point.
The principle is to first remove the P1 and convert it to a list, then remove the unwanted rows (values) from the list and then use them in the Dataframeisin()
In [9]: Ex_list = List (DF.P1) in [ten]: Ex_list.remove (' GD ') in [all]: Ex_list.remove (' HN ') in []: ex_listout[12]: [' SD ', ' HE N ', ' sh ']in []: Df[df.p1.isin (ex_list)]out[13]: p1 p2 p31 SD SX BJ3 HEN HEN HLJ4 SH TJ CQ
Related articles:
About Python in pandas. Dataframe add a new row and column to the row and column sample code
Python in Pandas. Brief introduction to DataFrame (Create, index, add and delete)