Access the elements of a Series in Pandas

Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type.

Let’s discuss different ways to access the elements of given Pandas Series.

First create a Pandas Series.



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# making data frame 
  
ser = pd.Series(df['Name'])
ser.head(10)
# or simply df['Name'].head(10)

chevron_right


Output:

 
Example #1: Get the first element of series

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# making data frame 
  
df['Name'].head(10)
  
# get the first element
ser[0]

chevron_right


Output:

 
Example #2: Access multiple elements by providing position of item

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# making data frame 
  
df['Name'].head(10)
  
# get multiple elements at given index
ser[[0, 3, 6, 9]]

chevron_right


Output:

 
Example #3: Access first 5 elements in Series

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# making data frame 
  
df['Name'].head(10)
  
# get first five names
ser[:5]

chevron_right


Output:

 
Example #4: Get last 10 elements in Series

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# making data frame 
  
df['Name'].head(10)
  
# get last 10 names
ser[-10:]

chevron_right


Output:

 
Example #5: Access multiple elements by providing label of index

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
import numpy as np
  
ser = pd.Series(np.arange(3, 15), index = list("abcdefghijkl"))
  
ser[['a', 'd', 'g', 'l']]

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.