Accessing Series Items in Pandas



Let`s discuss the different ways to access elements of this Pandas Series.

First, create a Pandas Series.

# import pandas module

import pandas as pd 

 
# create data frame

df = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 

ser = pd.Series (df [ `Name` ])

ser.head ( 10 )

# or just df [& # 39; Name & # 39;]. head (10)

Output:

Example # 1: Get the first element of the series

# pandas module import

import pandas as pd 

  
# create data frame

df = pd.read_csv ( " https:// media. python.engineering/wp-content/uploads/nba.csv "

  

df [  `Name` ]. head ( 10 )

  
# get first element

ser [ 0 ]

Output:

Example # 2: accessing multiple elements by providing element position

# pandas module import

import pandas as pd 

 
# create data frame

df =   pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 

df [ ` Name` ]. Head ( 10 )

 
# get multiple items at a given index

ser [[ 0 , 3 , 6 , 9 ]]

Output:

Example # 3: Accessing the first 5 elements in a series

# pandas module import

import pandas as pd 

 
# create data frame

df = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

  

df [ `Name` ]. head ( 10 )

 
# get the first five names

ser [: 5 ]

Output:

Example # 4: Get the last 10 items in a series

# pandas module import

import pandas as pd 

 
# create data frame

df = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 

df [ `Name ` ]. head ( 10 )

 
# get the last 10 names

ser [ - 10 :]

Output:

Example # 5: Accessing multiple items by providing an index label

# import 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` ]]

Output:
< img src = "http://espressocode.top/images/contpobanutrtranren494438.jpg" />