Q. Given a Series that stores the area of some states in km². Write code to find out the biggest and smallest three areas from the given Series. Given series has been created like this:


Ser1 = pd.Series ( [34567, 890, 450, 67892, 34677, 78902, 256711, 678291, 637632, 25723, 2367, 11789, 345, 256517])

Answer :-

import pandas as pd
Ser1 = pd.Series( [34567, 890, 450, 67892, 34677, 78902, 256711, 678291, 637632, 25723, 2367, 11789, 345, 256517])
print("Top 3 biggest areas are :")
print (Ser1.sort_values().tail(3))
print("3 smallest areas are :")
print(Ser1.sort_values().head (3))

#An alternative code for above problem would be:

import pandas as pd
Ser1 = pd.Series( [34567, 890, 450, 67892, 34677, 78902, 256711, 678291, 637632, 25723, 2367, 11789, 345, 256517])
print("Top 3 biggest areas are :")
print (Ser1.sort_values (ascending = False).head (3))
print("3 smallest areas are :")
print (Ser1.sort_values (ascending = False). tail(3))


Output:-

Top 3 biggest areas are :
6    256711
8    637632
7    678291
dtype: int64
3 smallest areas are :
12    345
2     450
1     890
dtype: int64

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post