简体   繁体   中英

What would be the syntactical classification of 'loc' and 'iloc' in pandas?

These are the questions I have:

  1. Syntatically speaking, what are loc and iloc in the pandas library? are they functions? methods? what are their specific classifications?

  2. Why do we use [] when using them?

  3. What do they do and what are they used for?

Sorry for the vagueness lack of clarity in the questions, and also, Thanks!

Both LOC and ILOC are methods as they're associated with the Pandas module.

In order to access values from rows and columns within a Dataframe, both LOC and ILOC are used. One can use these methods to filter and modify values within DF.

LOC - loc() is label based data selecting method which means that we have to pass the name of the row or column which we want to select. This method includes the last element of the range passed in it, unlike iloc().

ILOC - iloc() is a indexed based selecting method which means that we have to pass integer index in the method to select specific row/column. This method does not include the last element of the range passed in it unlike loc()

Example:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(10,100, (5, 4)), columns = list("ABCD"))

df.loc[1:3, "A":"C"]

before comma, the colon takes row selections and after comma, the colon takes column selections, here we've to specify the labels of the rows as well as the columns

df.iloc[1:3, 1:3] 

before comma, the colon takes row selections and after comma, the colon takes column selections, here we've to specify the index positions of the rows as well as the columns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM