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).