Pandas Data Index and selection

Source: Internet
Author: User

We choose the DataFrame from these three levels: rows, regions, cells.
The corresponding method of use is as follows:
A. Row, column--df[]
Two. Area--df.loc[], df.iloc[], df.ix[]
Three. Cell--df.at[], df.iat[]

Here's how to start the exercise:

Import NumPy as NP Import  = PD. DataFrame (Np.random.randn (6,4), index=list ('abcdef'), columns=list ('ABCD  '))

1. DF[]:

One-dimensional
Row dimension:
Integer slices, label slices, < Boolean arrays >
Levi degree:
Label index, label list, callable

Df[:3]df['a':'C']df[[true,true,true,false,false,false]]#first three rows (boolean array length equals number of rows)df[df['A']&GT;0]#rows with a column value greater than 0df[(df['A']&GT;0) | (df['B']&GT;0)]#a column with a value greater than 0, or a row with column B greater than 0df[(df['A']>0) & (df['C']&GT;0)]#rows with a column value greater than 0 and column C greater than 0

df['a']df[['a','B'  ]]df[Lambda#  callable

2. DF.Loc[]

Two-dimensional, first-after column
Row dimension:
Label index, label Slice, label list, < Boolean array >, callable
Levi degree:
Label index, label Slice, label list, < Boolean array >, callable

df.loc['a',:]df.loc['a':'D',:]df.loc[['a','b','C'],:]df.loc[[true,true,true,false,false,false],:]#first three rows (boolean array length equals number of rows)df.loc[df['A']>0,:]df.loc[df.loc[:,'A']>0,:]df.loc[df.iloc[:,0]>0,:]df.loc[Lambda_DF: _DF. A > 0,:]

df.loc[:,'A']df.loc[:,'A':'C']df.loc[:, ['A','B','C']]df.loc[:, [True,true,true,false]]#first three columns (boolean array length equals number of rows)df.loc[:, df.loc['a']&GT;0]#column A row is greater than 0df.loc[:, df.iloc[0]>0]#0 rows greater than 0 columnsdf.loc[:,Lambda_DF: ['A','B']]

Df. a.loc[Lambda s:s > 0]

3. DF.Iloc[]

Two-dimensional, pre-column
row dimension:
     integer index, integer slice, integer list, < Boolean array,
Levi:
    integer index, integer slice, integer list, < Boolean array >, callable

Df.iloc[3,:]df.iloc[:3,:]df.iloc[[0,2,4#  first three rows (boolean array length equals number of rows)DF.ILOC[DF ['A']>0,:]       #x why not? I can't figure it out! df.iloc[df.loc[:,'A'#xdf.iloc[df.iloc[:,0]>0,: ]  #xdf.iloc[Lambda _df: [0, 1],:]

df.iloc[:, 1]df.iloc[:, 0:3]df.iloc[:, [0,# # The first three columns (the length of the Boolean array equals the number of rows)Df.iloc [:, df.loc['a'#xdf.iloc[:, df.iloc[0]>0]  #x  Lambda _df: [0, 1]]

4. DF.IX[]

Two-dimensional, pre-column
row dimension:
     integer index, integer slice, integer list,
    tag index, label slice, label list,
    < Boolean array >,
     Callable
Levi:
     integer index, integer slice, integer list,
    tag index, label slice, label list,
    < Boolean array >,
     Callable

df.ix[0,:]df.ix[0:3,:]df.ix[[0,[+],:]df.ix['a',:] df.ix['a':'d',:]df.ix[['  a','b','C'],:

df.ix[:, 0]df.ix[:, 0:3]df.ix[:, [0,'A'  a':'C']df.ix[:, ['a ','B','C']


5. DF. at[]

Precise positioning of cells
Row dimension:
Label Index
Levi degree:
Label Index

df.at['a'a']

6. DF.IAT[]

Precise positioning of cells

Row dimension:
Integer index
Levi degree:
Integer index

Df.iat[0, 0]



Pandas Data Index and selection

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.