Pandas method for filtering data by combination criteria of a number of columns

Source: Internet
Author: User
This article mainly introduces the method of pandas to filter data according to the combination condition of several columns, has certain reference value, now share to everybody, the need friend can refer to

Or do you speak with a picture?

A file:

For example, I would like to filter out "design Wells", "put into production Wells", "current well" three columns of data are 11 data, the results are as follows:

Of course, the filter conditions here can be adjusted according to the user's needs, the code is as follows:

#-*-Coding:utf-8-*-"" "Created on Wed Nov 10:46:31 2017@author:wq" "" Import pandas as Pd#input.csv is that large file, there are many many rows df1 = Pd.read_csv (U ' input.csv ', encoding= ' GBK ') #加encoding = ' GBK ' is due to the existence of Chinese in the file, without the possibility of garbled # filter conditions here can be modified according to user needs outfile = df1[( Df1[u ' design Wells ']== ' One ') & (Df1[u ' Put into production Wells ']== ') & (Df1[u ' current Wells ']== ')]outfile.to_csv (' Outfile.csv ', index=false , encoding= ' GBK ')

Sometimes we also have the opposite demand, need to delete "design well", "put into production well", "current well" three columns of data are 11 of those rows, the effect is as follows:

The code is as follows:

#input. csv is that large file, there are many lines df1 = pd.read_csv (U ' input.csv ', encoding= ' GBK ') df2 = pd.read_csv (U ' outfile.csv ', encoding= ' GBK ') #加encoding = ' GBK ' is due to the existence of Chinese in the file, without the possibility of garbled index = ~df1[u ' Kanji well number '].isin (df2[u ' kanji Well Number ')) Df4 = Df1[index]df4.to_csv (' Outfile1.csv ', Index=false, encoding= ' GBK ')
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.