Detailed in Python pandas. Dataframe example code to exclude a specific line method

Source: Internet
Author: User
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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.