Examples of how Python uses pandas to query data

Source: Internet
Author: User
Querying and analyzing data is an important function of pandas, is also the basis of our learning pandas, the following article mainly introduces you about how to use the data analysis of Python pandas query data, the text through the sample code introduced in very detailed, the needs of friends can reference , let's take a look below.

Objective

In the field of data analysis, the most popular is the Python and R language, this article will give you a detailed introduction of the Python Use Pandas query data related content, share it for everyone to reference the study, the following words do not say, come together to see the detailed introduction bar.

Sample code

The query data here is equivalent to the subset function in the R language, which can be used to select a subset of the original data, a specified row, a specified column, etc. by using a Boolean index. Let's first import a student dataset:


Student = Pd.io.parsers.read_csv (' c:\\users\\admin\\desktop\\student.csv ')

Query the first 5 rows of data or the end 5 lines:


Student.head () Student.tail ()

Query the specified row:


student.ix[[0,2,4,5,7]] #这里的ix索引标签函数必须是中括号 []

Query the specified column:


student[[' Name ', ' Height ', ' Weight ']].head () #如果多个列的话, must use double brackets

You can also query the specified columns by using the IX index label:


student.ix[:,[' Name ', ' Height ', ' Weight ']].head ()

Query the specified rows and columns:


student.ix[[0,2,4,5,7],[' Name ', ' Height ', ' Weight ']].head ()

Find information for all girls:


student[student[' Sex ']== ' F '

Check out all information about girls over 12 years of age:


student[(student[' Sex ']== ' F ') & (student[' age ']>12)]

Find out the names, height and weight of all girls over the age of 12:


student[(student[' Sex ']== ' F ') & (student[' age ']>12)][[' Name ', ' Height ', ' Weight ']

The query logic above is actually very simple, it should be noted that if the query is more than one condition, it must be enclosed in parentheses on both sides of & (and) or | (OR).

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.