The square brackets are syntactic sugar for the special method __getitem__ . All objects can implement this method in their class definition and then subsequently work with the square brackets.
The main difference between pandas loc[] vs iloc[] is loc gets DataFrame rows & columns by labels/names and iloc[] gets by integer Index/position. For loc[], if the label is not present it gives a key error. For iloc[], if the position is not present it gives an index error.
Conclusion. When you'd like to access just one value in a pandas DataFrame, both the loc and at functions will work fine. However, when you'd like to access a group of rows and columns, only the loc function is able to do so.
The loc() function helps us to retrieve data values from a dataset at an ease. Using the loc() function, we can access the data values fitted in the particular row or column based on the index value passed to the function.
In the following situations, they behave the same:
df['A']
is the same as df.loc[:, 'A']
-> selects column A)df[['A', 'B', 'C']]
is the same as df.loc[:, ['A', 'B', 'C']]
-> selects columns A, B and C)df[1:3]
is the same as df.iloc[1:3]
-> selects rows 1 and 2. Note, however, if you slice rows with loc
, instead of iloc
, you'll get rows 1, 2 and 3 assuming you have a RangeIndex. See details here.)However, []
does not work in the following situations:
df.loc[row_label]
df.loc[[row_label1, row_label2]]
df.loc[:, 'A':'C']
These three cannot be done with []
.
More importantly, if your selection involves both rows and columns, then assignment becomes problematic.
df[1:3]['A'] = 5
This selects rows 1 and 2 then selects column 'A' of the returning object and assigns value 5 to it. The problem is, the returning object might be a copy so this may not change the actual DataFrame. This raises SettingWithCopyWarning. The correct way of making this assignment is:
df.loc[1:3, 'A'] = 5
With .loc
, you are guaranteed to modify the original DataFrame. It also allows you to slice columns (df.loc[:, 'C':'F']
), select a single row (df.loc[5]
), and select a list of rows (df.loc[[1, 2, 5]]
).
Also note that these two were not included in the API at the same time. .loc
was added much later as a more powerful and explicit indexer. See unutbu's answer for more detail.
Note: Getting columns with []
vs .
is a completely different topic. .
is only there for convenience. It only allows accessing columns whose names are valid Python identifiers (i.e. they cannot contain spaces, they cannot be composed of numbers...). It cannot be used when the names conflict with Series/DataFrame methods. It also cannot be used for non-existing columns (i.e. the assignment df.a = 1
won't work if there is no column a
). Other than that, .
and []
are the same.
loc
is specially useful when the index is not numeric (e.g. a DatetimeIndex) because you can get rows with particular labels from the index:
df.loc['2010-05-04 07:00:00']
df.loc['2010-1-1 0:00:00':'2010-12-31 23:59:59 ','Price']
However []
is intended to get columns with particular names:
df['Price']
With []
you can also filter rows, but it is more elaborated:
df[df['Date'] < datetime.datetime(2010,1,1,7,0,0)]['Price']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With