Thursday, September 16, 2021

Series Creation in Pandas - 3

 Q - 1

Create an Empty Series using Pandas.


import pandas as pd

S=pd.Series()

print(S)


Output:


Series([], dtype: float64)



 Q - 2

Create a series using List.


import pandas as pd

L=[5,8,10,15,20]

S=pd.Series(L)

print(S)


Output:


0     5

1     8

2    10

3    15

4    20

dtype: int64



 Q - 3

Create a series using two different List.


import pandas as pd

Months=['Jan','Feb','Mar','Apr','May']

Days=[31,28,31,30,31]

S=pd.Series(Days,Months)

print(S)


Output:


Jan    31

Feb    28

Mar    31

Apr    30

May    31

dtype: int64



Q - 4

Create a series using range() method.


import pandas as pd

I=list(range(1,6))

D=list(range(2,11,2))

S=pd.Series(D,I)

print(S)


Output:

1     2

2     4

3     6

4     8

5    10

dtype: int64


Q - 5

Create a Series using range() and for loop.


import pandas as pd

I=[x for x in range(1,6)]

D=[x for x in range(3,16,3)]

S=pd.Series(D,I)

print(S)


Output:

1     3

2     6

3     9

4    12

5    15

dtype: int64



Q - 6

Handling floating point values for generating a Series.



import pandas as pd

I=[x for x in range(1,6)]

D=[2,5,8,4,9.6]

S=pd.Series(D,I)

print(S)


Output:

1    2.0

2    5.0

3    8.0

4    4.0

5    9.6

dtype: float64


Q - 7

Creating a Series using missing values (NaN)



import pandas as pd

import numpy as np

L=[2,3,5,7,8]

S=pd.Series([8,7.5,4.9,np.NaN,9])

print(S)


Output:


0    8.0

1    7.5

2    4.9

3    NaN

4    9.0

dtype: float64



Q - 8

Creating a Series and modify the index values.



import pandas as pd

S=pd.Series([31,28,31,30,31])

print(S)

S.index=['Jan','Feb','Mar','Apr','May']

print(S)


Output:

0    31

1    28

2    31

3    30

4    31

dtype: int64

Jan    31

Feb    28

Mar    31

Apr    30

May    31

dtype: int64



Q - 9

Creating a Series and access first and third element

import pandas as pd

D=[10,30,50,80,90]

S=pd.Series(D)

print(S)

print("First Element\n",S[0])

print("Third Element\n",S[2])


Output:


0    10

1    30

2    50

3    80

4    90

dtype: int64

First Element

 10

Third Element

 50

Q - 10

Creating a Series of 5 Elements and print complete Series, 2nd Element, and First 3 Element


import pandas as pd

D=[10,30,50,80,90]

S=pd.Series(D)

print(S)

print("Second Element\n",S[1])

print("First Three Element\n",S[0:3])

print("First Three Element\n",S[:3])


Output:


0    10

1    30

2    50

3    80

4    90

dtype: int64

Second Element

 30

First Three Element

 0    10

1    30

2    50

dtype: int64

First Three Element

 0    10

1    30

2    50

3    80

4    90

dtype: int64


Q - 11

Creating a Series of 5 Elements and perform slicing on Series


import pandas as pd

D=[10,20,30,40,50]

I=['a','b','c','d','e']

S=pd.Series(D,I)

print(S)

print(S['a'])

print(S[0:3])

print(S[-3:])


Output:

a    10
b    20
c    30
d    40
e    50
dtype: int64
10
a    10
b    20
c    30
dtype: int64
c    30
d    40
e    50
dtype: int64



No comments:

Post a Comment