Accessing Pandas Series Items



In this article we use the file “ nba.csv “to download CSV, click here .

Access to an element from a series with a position

An index number is used to access an element of a series. Use the index operator [] to access an item in a series. The index must be an integer.
To access several elements from the series, we use the Slice operation. The Slice operation is performed on a series using the colon (:). To print items from start to range, use [:Index ] to print items from end use [:-Index ] , to print items from a specific index to the end, use [Index:] to print items within a range use [Start Index: End Index] and to print an entire series using a slicing operation use [:> . Next, to print the entire series in reverse order, use [::-1 ] .

Code # 1: Accessing the first element of the series

# pandas and nudies import

import pandas as pd

import numpy as np

 
# create a simple array

data = np.array ([ `g` , ` e` , `e` , ` k` , `s` , ` f` , `o` , `r` , ` g` , `e` , `e` , ` k` , `s` ])

ser = pd.Series (data)

 

 
# get first element

print (ser [ 0 ] )

Output:

 g 

Code no. 2: Access the first 5 elements of the series

# import of pandas and nudies

import pandas as pd

import numpy as np

  
# create a simple array

data = np.array ([ `g` , ` e` , `e` , ` k` , `s` , ` f` , `o` , `r` , ` g` , `e` , ` e` , `k` , ` s` ])

ser = pd.Series (data)

 

 
# get first element

print (ser [: 5 ])

Output:

Code # 3: Access to the last 10 elements of the series

# pandas and nudies import

import pandas as pd

import numpy as np

 
# create a simple array

data = np.array ([ `g` , ` e` , `e` , `k` , `s` , ` f` , `o` , ` r` , ` g` , `e` , `e` , `k` , ` s` ])

ser = pd.Series (data)

 

 
# get the first element

print (ser [ - 10 :])

Output:

Code # 4: Accessing the first 5 Series elements in nba.csv

# pandas module import

import pandas as pd 

 
# create data frame

df = pd.read_csv ( "nba.csv"

 

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

ser.head ( 10


We now access the first 5 e series elements

# get the first five names

ser [: 5

Output:

Accessing an element by label (index)

To access an element from a series, we have to set values ​​by the index label. A series is like a fixed-size dictionary where you can get and set values ​​by an index mark.

Code # 1: Accessing a single item using an index mark

Output:

 o 

Code # 2: Accessing multiple elements with an index mark

# pandas and nudies import

import pandas as pd

import numpy as np

 
# create a simple array

data = np.array ([ `g` , `e` , ` e` , `k` , ` s` , `f` , `o` , ` r` , ` g` , `e` , `e` , ` k` , `s` ])

ser = pd.Series (data, index = [ 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 ])

 

 
# access the element using the index element

print ( ser [ 16 ])

# pandas and nudies import

import pandas as pd

import numpy as np

 
# create a simple array

data = np.array ([ `g` , ` e` , ` e` , `k` , `s` , `f` , ` o` , `r` , ` g` , `e` , ` e` , `k` , ` s` ])

ser = pd.Series (data, index = [ 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 ])

 

 
# access multiple items with
# index element

print (ser [[ 10 , 11 , 12 , 13 , 14 ]])

Output:

Code # 3: Access to multiple items by providing an index label

# pandas and numpy imports

import pandas as pd 

import numpy as np 

 

ser = pd.Series (np.arange ( 3 , 9 ), index = [ `a` , ` b` , `c` , ` d` , `e` , `f` ]) 

  

print (ser [[ `a` , `d` , `g` , ` l` ]])

Output:

Code # 4: Multiple access elements with an index mark in nba.csv

# pandas module import

import pandas as pd 

 
# create data frame

df = pd.read_csv ( "nba.csv"

 

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

ser.head ( 10


We now access multiple elements using the index mark

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

Output: