Basic operations on pandas. DataFrame in python

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

Preface

When you use Python for data analysis, a frequently used data structure is pandas DataFrame. for details about the basic operations of pandas. DataFrame in python, refer to this article.

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

Summary

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.