Pandas+dataframe implementing row and column selection and slicing operations

Source: Internet
Author: User
This time to bring you pandas+dataframe to achieve the choice of row and slice operation, pandas+dataframe to achieve the row and column selection and the attention of the slicing operation, the following is the actual case, take a look.

Select in SQL is selected according to the name of the column, pandas is more flexible, not only can be selected according to the column name, but also according to the column position (number, in the first few rows, note that the position of the Pandas column is starting from 0) selection. The relevant functions are as follows:

1) Loc, based on the column label, can select a specific line (based on the row index);

2) Iloc, position based on row/column;

3) at, according to the specified row index and column label, quickly locate the elements of the dataframe;

4) IAT, similar to at, the difference is based on the position to locate;

5) IX, as a mixture of LOC and Iloc, supports both label and position;

Instance

Import pandas as Pdimport numpy as Npdf = PD. DataFrame ({' Total_bill ': [16.99, 10.34, 23.68, 23.68, 24.59],          ' tip ': [1.01, 1.66, 3.50, 3.31, 3.61],          ' sex ': [' Fem Ale ', ' Male ', ' Male ', ' Male ', ' Female ']}) # data type of Columnsprint df.dtypes# indexesprint df.index# return pandas. Indexprint df.columns# each row, return Array[array]print Df.valuesprint DF
Sex      objecttip      float64total_bill  float64dtype:objectrangeindex (start=0, stop=5, Step=1) Index ([u ' sex ', U ' tip ', U ' Total_bill '], dtype= ' object ') [[' Female ' 1.01 16.99] [' Male ' 1.66 10.34] [' Male ' 3.5 23.68] [' Male ' 3.31 23.68] [ ' Female ' 3.61 24.59]]   sex  tip total_bill0 Female 1.01    16.991  Male 1.66    10.342  Male 3.50    23.683  Male 3.31    23.684 Female 3.61    24.59
Print Df.loc[1:3, [' Total_bill ', ' Tip ']]print df.loc[1:3, ' tip ': ' Total_bill ']print df.iloc[1:3, [1, 2]]print df.iloc[ 1:3, 1:3]
  Total_bill  tip1    10.34 1.662    23.68 3.503    23.68 3.31  tip total_bill1 1.66    10.342    3.50 23.683 3.31    23.68  tip total_bill1 1.66    10.342 3.50    23.68  tip total_bill1 1.66    10.342 3.50    23.68

Incorrect representation:

Print Df.loc[1:3, [2, 3]]#.loc only supports column name operations
Keyerror: ' None of [[2, 3]] is in the [columns] '
Print df.loc[[2, 3]]#.loc can be selected without a column name.
  Sex  tip total_bill2 Male 3.50    23.683 Male 3.31    23.68
Print Df.iloc[1:3]#.iloc can be the row selection without adding the first column
Sex  tip total_bill1 Male 1.66    10.342 Male 3.50    23.68
Print Df.iloc[1:3, ' tip ': ' Total_bill ']
Typeerror:cannot do slice indexing on <class ' Pandas.indexes.base.Index ' > with these indexers [tip] of <type ' St R ' >
Print df.at[3, ' Tip ']print df.iat[3, 1]print df.ix[1:3, [1, 2]]print df.ix[1:3, [' Total_bill ', ' tip ']
3.313.31  tip total_bill1 1.66    10.342 3.50    23.683 3.31    23.68  total_bill  tip1    10.34 1.662    23.68 3.503    23.68 3.31
Print df.ix[[1, 2]] #行选择
  Sex  tip total_bill1 Male 1.66    10.342 Male 3.50    23.68
Print Df[1:3]print df[[' total_bill ', ' Tip ']]# print df[1:2, [' Total_bill ', ' tip ']] # typeerror:unhashable type
Sex  tip total_bill1 Male 1.66    10.342 Male 3.50    23.68  total_bill  tip0    16.99 1.011    10.34 1.662    23.68 3.503    23.68 3.314    24.59 3.61
Print Df[1:3,1:2]
Typeerror:unhashable type

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

What are the methods of dataframe queries in pandas

Selenium+cookie Skip Verification Code Login Implementation step

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.