How To Get Value From Pandas Series?

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 attribute to retrieve the data from the pandas Series.

In this article, I will explain how to get values from Pandas Series using its index/index labels and also explain using Series.get() function, Series.values, and loc[] attributes how we can access data from Pandas Series.

1. Quick Examples to Get Value from Series

Following are quick examples of getting value 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 that is 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. A List, NumPy Array, and Dict can be turned into a pandas Series.

3. Get Values from Pandas Series by Using Index

The label is used to access a specified value from the pandas Series. The values are labeled with their index number. The first value has index 0, the second value has index 1, etc.

Pandas Series can create in several ways by using Python list & dictionaries, below example create a Series from a list. In order 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(series)

Pandas by default add a sequence number to the Series.


# Output:
0          Spark
1          PySpark
2          Hadoop
3          Python
4          pandas
5           Oracle
dtype:   object

Let’s get the specified value from Series by using its index.


# Get value from Series using index
print(series[2])

# Output:
# Hadoop

4. Get Values from 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.

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 wanted to convert Pandas Series to the dictionary use the Series.to_dict() function.


# Create Series using dictionary
data = {'Courses' :"pandas", 'Fees' : 20000, 'Duration' : "30days"}
series = pd.Series(data)
print (series)

# Output: 
# 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(value)

# Output:
# 20000

5. Get Value from Pandas Series using get()

Using the Series.get() method we can 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(value)

# Output:
# 20000

6. Get Value from Series using values Attribute

Using the Series.values attribute we can get the values of given Series in the form of Numpy array representation where the values are all elements of the Series.


# Get values from using .values
print (series.values)

# Output:
# ['pandas' 20000 '30days']

7. Get Value from Series using loc[] & values Attribute

By using the loc and values attribute, we 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 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

8. Conclusion

In this article, I have explained how to get value from Pandas Series using its index/index labels and also explained using various functions and attributes how we can access the data form Pandas Series.

Happy learning!!

References

Leave a Reply