Friday, September 17, 2021

Series Creation in Pandas - 4

 Q:1 

Create a Series and Explain the use of head() and tail()


import pandas as pd

D=[10,20,30,40,50,60,70,80,90]

I=['a','b','c','d','e','f','g','h','i']

S=pd.Series(D,I)

print(S)

print(S.head())  #print first 5 elements

print(S.tail())     #print last 5 elements

print(S.head(3))    #print first 3 elements

print(S.tail(3))    #print last elements

print(S.head(-2))   #print first n-2 elements (not print last 2 elements)

print(S.tail(-2))   #print last n-2 elements (not print first 2 elements)


Output:

a    10
b    20
c    30
d    40
e    50
f    60
g    70
h    80
i    90
dtype: int64
a    10
b    20
c    30
d    40
e    50
dtype: int64
e    50
f    60
g    70
h    80
i    90
dtype: int64
a    10
b    20
c    30
dtype: int64
g    70
h    80
i    90
dtype: int64
a    10
b    20
c    30
d    40
e    50
f    60
g    70
dtype: int64
c    30
d    40
e    50
f    60
g    70
h    80
i    90
dtype: int64

No comments:

Post a Comment