How can I get a value from a Pandas Series in Python?

How can I get a value from a Pandas Series in Python?

To retrieve a specific value from a Pandas Series in Python, you can use the indexing method by specifying the index or label of the desired element. Alternatively, you can use the .iloc[] or .loc[] methods to access values by their numerical or label-based positions. Additionally, you can use conditional indexing to retrieve values that meet certain criteria. These methods allow for easy and efficient retrieval of values from a Pandas Series in Python.

Pandas: Get Value from Series (3 Examples)


The following examples show how to get a value from a pandas Series in three different scenarios.

Method 1: Get Value from Pandas Series Using Index

The following code shows how to get the value in the third position of a pandas Series using the index value:

import pandas as pd

#define Series
my_series = pd.Series(['A', 'B', 'C', 'D', 'E'])

#get third value in Series
print(my_series[2])

C

By specifying the index value 2, we’re able to extract the value in the third position of the pandas Series.

Method 2: Get Value from Pandas Series Using String

The following code shows how to get the value that corresponds to a specific string in a pandas Series:

import pandas as pd

#define Series
my_series = pd.Series({'First':'A', 'Second':'B', 'Third':'C'})

#get value that corresponds to 'Second'
print(my_series['Second'])

B

Using this syntax, we’re able to get the value that corresponds to ‘Second’ in the pandas Series.

Method 3: Get Value from Pandas Series in DataFrame

The following code shows how to get the value in a pandas Series that is a column in a pandas DataFrame

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Spurs', 'Rockets', 'Heat', 'Nets'],
                   'points': [100, 114, 121, 108, 101]})

#view DataFrameprint(df)

      team  points
0     Mavs     100
1    Spurs     114
2  Rockets     121
3     Heat     108
4     Nets     101

#get 'Spurs' value from team column
df.loc[df.team=='Spurs','team'].values[0]

'Spurs'

By using the loc and values functions, we’re able to get the value ‘Spurs’ from the DataFrame.

Related:

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Cite this article

stats writer (2024). How can I get a value from a Pandas Series in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-get-a-value-from-a-pandas-series-in-python/

stats writer. "How can I get a value from a Pandas Series in Python?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-get-a-value-from-a-pandas-series-in-python/.

stats writer. "How can I get a value from a Pandas Series in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-get-a-value-from-a-pandas-series-in-python/.

stats writer (2024) 'How can I get a value from a Pandas Series in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-get-a-value-from-a-pandas-series-in-python/.

[1] stats writer, "How can I get a value from a Pandas Series in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I get a value from a Pandas Series in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top