This article mainly introduces you to the pandas in Python. Dataframe to exclude specific lines of the method, the text gives a detailed example code, I believe that everyone's understanding and learning has a certain reference value, the need for friends to see together below.
Objective
When you use Python for data analysis, one of the most frequently used structures is the dataframe of pandas, about pandas in Python. Dataframe basic operation, you can view this article.
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
Summarize
"Recommended"
1. Python Free video tutorial
2. Python Basics Getting Started tutorial
3. Python meets data collection video tutorial