We can get the values from the Pandas Series by using its numeric index
or index labels
. In Series every element contains the corresponding index/index labels, by using these indexes we can get the values from Pandas Series. Pandas provide various Series methods and attributes to retrieve the data from Series.
In this article, I will explain how to retrieve values from a Pandas Series using its index position or index labels. I will also demonstrate how to access data from a Pandas Series using various attributes and functions.
Key Points –
- Utilize integer-based positional indexing or label-based indexing to access individual elements or subsets of a Pandas Series.
- Access Series elements using dot notation with attribute names or bracket notation with string keys, providing flexibility in retrieving data.
- Perform arithmetic operations directly on Series objects, enabling quick computations and transformations.
- Leverage built-in functions like mean, median, min, max, and standard deviation to gain insights into the data’s distribution.
- Filter data based on conditions using boolean indexing, facilitating selective retrieval of relevant information.
1. Quick Examples to Get Value from Series
Following are quick examples of getting values from the Series.
# Below are the quick examples
# Example 1: Get value from Series
# Using index
print(series[2])
# Example 2: Get the Series value
# Using index labels
value = series['Fees']
# Example 3: Get value from Series
# Using get()
value = series.get(key = 'Fees')
# Example 4: Get values from using Series.values
print (series.values)
# Example 5: Get value from DataFrame column
print(df.loc[df.Duration == '50days','Duration'].values[1])
2. What is Pandas Series?
Pandas Series is a one-dimensional array capable of storing various data types (integer, string, float, python objects, etc.). We can easily convert the list, tuple, and dictionary into Series using the pandas.Series()
function. The row labels of the Series are called the index. The Series can have only one column.
3. Get Values from the Pandas Series by Using the Index
You can use the default integer index to access a specified value from the pandas Series. The values are labeled with their index number. The first value has an index of 0
, the second value has an index of 1
, and so on.
Pandas Series can be created in several ways by using Python lists & dictionaries, below example create a Series from a list. To use Pandas first, you need to import using import pandas as pd
.
# create Pandas Series
import pandas as pd
list = ["Spark","PySpark","Hadoop","Python","pandas","Oracle"]
series = pd.Series(list )
print("Create Pandas Series:\n", series)
Pandas by default add a sequence number to the Series.
Let’s get the specified value from the Series by using its index position.
# Get value from Series using index
print("Get the specified value of Series by index:\n", series[2])
Yields below output.
You can also get multiple values of Series by index. You can use the list of indexes to access multiple values from Pandas Series. When we access a list of specified elements from the Series, it will return the Series where the values are specified elements.
# Get multiple values from Series
print("Get multiple values of Series by index:\n", series[[2, 4, 1]])
# Output:
# Get multiple values of Series by index:
# 2 Hadoop
# 4 pandas
# 1 PySpark
# dtype: object
4. Get Values from the Pandas Series by Using Index Labels
Alternatively, we can get the values from Pandas Series by using index labels. for that, we need to set the customized index labels to Series.
A Python Dictionary can be used to create Pandas Series. Keys from Dict are used as Index and values are used as a column in Pandas Series. In case you want to convert Pandas Series from the dictionary use the Series.to_dict()
function.
# Create Series using dictionary
data = {'Courses' :"pandas", 'Fees' : 20000, 'Duration' : "30days"}
series = pd.Series(data)
print("Create Pandas Series:\n", series)
# Output:
# Create Pandas Series:
# Courses pandas
# Fees 20000
# Duration 30days
# dtype: object
Let’s use index labels and get the specified value from Series.
# Get the Series value using index labels
value = series['Fees']
print("Get the specified value of Series by index:\n", value)
# Output:
# Get the specified value of Series by index:
# 20000
5. Get Value from Pandas Series using get()
Similarly, you can use the Series.get()
method to retrieve the value from Pandas Series. Set key
Param with a specified index of given Series and pass into get()
function, it will return the specified value of given Series.
# Get value from Series using get()
value = series.get(key = 'Fees')
print("Get the specified value of Series by index:\n", value)
# Output:
# Get the specified value of Series by index:
# 20000
6. Get Value from Series using values Attribute
You can also use the Series.values
attribute to get the values of a given Series in the form of a Numpy array representation where the values are all elements of the Series.
# Get values from using .values
print("Get the values from the series:\n", series.values)
# Output:
# Get the values from the series:
# ['pandas' 20000 '30days']
7. Get Value from Series using loc[] & values Attribute
By using the loc[]
and values
attributes, you can get the Specified value of the DataFrame column. Series is nothing but a column of a DataFrame so, let’s create DataFrame and apply these attributes to the column of the DataFrame.
# Create DataFrame
technologies= ({
'Courses':["Spark","PySpark","Hadoop","Pandas","Spark","PySpark", "Pandas"],
'Fee': [22000,25000,30000,35000,22000,25000,35000],
'Duration':['30days','50days','40days','35days','30days','50days','60days'],
'Discount':[1000,2000,2500,1500,1000,2000,1500]
})
index_labels=['r1','r2','r3','r4','r5','r6','r7']
df = pd.DataFrame(technologies, index=index_labels)
print(df)
# Get value from DataFrame column
print(df.loc[df.Duration == '50days','Duration'].values[1])
# Output:
# 50days
Frequently Asked Questions of Get Value From Pandas Series
To access a specific element in a Pandas Series, you can use either integer-based positional indexing or label-based indexing. For example, series_name[index]
.
You can use a list of indexes to access multiple elements from the pandas Series. For example, series_name[[index1, index2, index3]]
To retrieve all values from a Pandas Series, you can simply access the Series object itself. If you print the Series or access its values attribute, you’ll get all the values contained within it. For example, series_name.values
.
df.loc[]
is label-based indexing, and df
.iloc[] is integer-location-based indexing. You can use .loc[]
it to access elements by their labels, and you can use.iloc[]
it to access them by their integer position.
You can use the in
operator or the .isin()
method to check the values are exist in a Series. For example: value in series_name
or series_name.isin([value])
.
Conclusion
In this article, I have explained how to get specified values from the Pandas Series using its integer index/index labels and also explained using various functions and attributes how we can access the data from the Pandas Series with multiple examples.
Happy learning!!
Related Articles
- Pandas Series where() Function
- Use pandas.to_numeric() Function
- How to Get Index of Series in Pandas
- Pandas Remove Elements From Series
- Pandas Series filter() Function
- How to reshape the Pandas Series?
- Pandas Series.isin() Function
- Pandas.Series.combine()
- Pandas Series.diff() Function
- How to Sort Pandas Series
- How to replace the Pandas Series?
- How to append Pandas Series?
- How to rename Pandas Series?
- Pandas Series.quantile() Function
- How to get floor or ceil values of Pandas Series?
- Apply Multiple Filters to Pandas DataFrame or Series
- How to Convert NumPy Array to Pandas Series
- Convert Series to Dictionary(Dict) in Pandas
- How to Merge Series into Pandas DataFrame