SoFunction
Updated on 2024-11-13

Python data analysis of how to use pandas query data sample code

preamble

In the field of data analysis, the most popular than Python and R language, this article will give you a detailed introduction to the use of Python pandas query data related content, shared for your reference and learning, the following words do not say much, come together to look at the detailed introduction.

sample code (computing)

The query data here is equivalent to the subset function in R. You can select a subset of the original data, a specified row, a specified column, etc., by using a Boolean index to target them. We start by importing a student dataset:

student = .read_csv('C:\\Users\\admin\\Desktop\\')

Queries the first 5 rows or the last 5 rows of the data:

()
()

Queries the specified row:

[[0,2,4,5,7]] #Here.ixThe index tag function must be a center bracket[]

Queries the specified column:

student[['Name','Height','Weight']].head() #If multiple columns,Double middle brackets must be used

Specified columns can also be queried by ix index tags:

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

Queries the specified rows and columns:

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

Check all the girls' information:

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

Find out all the information about girls over 12 years of age:

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

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

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

The above query logic is actually very simple, it should be noted that, if it is more than one condition of the query, you must be in & (and) or | (or) at the end of the two conditions enclosed in parentheses.

summarize

The above is the entire content of this article, I hope that the content of this article on your learning or work can bring some help, if there are questions you can leave a message to exchange, thank you for my support.