- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to access pandas Series elements using the .loc attribute?
The “.loc” is an attribute of the pandas.Series object which is used to access elements from series based on label indexing. And It works similar to pandas.Series “at” attribute but the difference is, the “at” attribute accesses only a single element whereas the “loc” attribute can access a group of elements using labels.
The “loc” attribute accesses the labels based on labels and it also supports slicing object with labels.
Example 1
import pandas as pd import numpy as np # creating pandas Series object series = pd.Series({'B':'black', 'W':'white','R':'red', 'Bl':'blue','G':'green'}) print(series) print("Output: ") print(series.loc['B'])
Explanation
In this following example, we created a pandas series object “series” using a python dictionary with pairs of keys and values. Here the index labels are created by using keys in the dictionary.
Output
B black W white R red Bl blue G green dtype: object Output: black
We have successfully accessed a single element from pandas.Series object ”series” by using the label “B”. The label “B” is given to the loc attribute.
Example 2
import pandas as pd import numpy as np # creating pandas Series object series = pd.Series({'B':'black', 'W':'white','R':'red', 'Bl':'blue','G':'green'}) print(series) print("Output: ") print(series.loc['B':'G'])
Explanation
In the following example, we will access the group of elements from the pandas.Series object by providing a slicing object to the “loc” attribute.
Output
B black W white R red Bl blue G green dtype: object Output: B black W white R red Bl blue G green dtype: object
We have accessed the group of pandas.Series elements by using the “loc” attribute. And we got another series object as a result which is displayed in the above output block. It will raise KeyError if the provided labels are not present in the series object.
- Related Articles
- How to access pandas DataFrame elements using the .loc attribute?
- How to access pandas Series elements using the .iloc attribute?
- How to access pandas DataFrame elements using the .iloc attribute?
- How to access Pandas Series elements by using indexing?
- How to access a group of elements from pandas Series using the .iloc attribute with slicing object?
- How to access a single value in pandas Series using the .at attribute?
- How to access datetime indexed elements in pandas series?
- How to access a single value in pandas Series using the integer position?
- How to access a single value in pandas Data Frame using the .at attribute?
- How to append a list to a Pandas DataFrame using loc in Python?
- Python - How to access the last element in a Pandas series?
- How to append elements to a Pandas series?
- How are iloc and loc different in Python Pandas?
- How to compare elements of a series by Python list using pandas series.ge() function?
- How to compare elements of a series by Python list using pandas series.gt() function?
