Sunday, December 29, 2024
Google search engine
HomeLanguagesHow to access the last element in a Pandas series?

How to access the last element in a Pandas series?

Prerequisite: PandasĀ 

Pandas series is useful in handling various analytical operations independently or as being a part of pandas data frame. So it is important for us to know how various operations are performed in pandas series. The following article discusses various ways in which last element of a pandas series can be retrieved.

Method 1: Naive approachĀ 

There are two naive approaches for accessing the last element:

  • Iterate through the entire series till we reach the end.
  • Find the length of the series. The last element would be length-1 (as indexing starts from 0).

Program:

Python3




# importing the pandas library
import pandas as pd
Ā Ā 
# initializing the series
ser = pd.Series(['g', 'e', 'e', 'k', 's'])
Ā Ā 
# iterating the series until the iterator reaches the end of the series
for i in range(0, ser.size):
Ā Ā Ā Ā if i == ser.size-1:
Ā Ā Ā Ā Ā Ā Ā Ā # printing the last element i.e, size of the series-1
Ā Ā Ā Ā Ā Ā Ā Ā print("The last element in the series using loop is : ", ser[i])
Ā Ā 
# calculating the length of the series
len = ser.size
Ā Ā 
# printing the last element i.e len-1 as indexing starts from 0
print("The last element in the series by calculating length is : ", ser[len-1])


Output:

Method 2: Using .iloc or .iat

Pandas iloc is used to retrieve data by specifying its integer index. In python negative index starts from end therefore we can access the last element by specifying index to -1 instead of length-1 which will yield the same result.Ā 

Pandas iat is used to access data of a passed location. iat is comparatively faster than iloc. Ā Also note that ser[-1] will not print the last element of series, as series supports positive indexes only. However, we can use negative indexing in iloc and iat.

Program:

Python3




# importing the pandas library and time
import pandas as pd
import time
# initializing the series
ser = pd.Series(['g', 'e', 'e', 'k', 's'])
Ā Ā 
start = time.time()
print("The last element in the series using iloc is : ", ser.iloc[-1])
end = time.time()
Ā Ā 
print("Time taken by iloc : ", end-start)
Ā Ā 
start = time.time()
print("The last element in the series using iat is : ", ser.iat[-1])
end = time.time()
Ā Ā 
print("Time taken by iat : ", end-start)


Output:

Method 3: Using tail(1).item()

tail(n) is used to access bottom n rows from a series or a data frame and item() returns the element of the given series object as scalar.

Program:

Python3




# importing the pandas library
import pandas as pd
# initializing the series
ser = pd.Series(['g', 'e', 'e', 'k', 's'])
Ā Ā 
# printing the last element using tail
print("The last element in the series using tail is : ", ser.tail(1).item())


Output:

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?