Professional Documents
Culture Documents
import pandas as pd
a=pd.Series(range(7,11))
print(a)
Example 3:
import pandas as pd
a=pd.Series(range(2,10,2),index=['a',1,'b',2])
print(a)
Q1) Write a python code to create the series data structure by using range().
Answer:
import pandas as pd
a=pd.Series(range(3,15,4))
print(a)
2) By using Dictionary
Example for dictionary (Key –Value pair)
D={‘a’: ‘apple’, 1: ‘One’, ‘b’: ‘banana’, 2: ‘Two’}
Example1:
import pandas as pd
d={1:'One',2:'Two',3:'Three'}
s=pd.Series(d)
print(s) NOTE: string’s data type is object
Example2:
Consider a given Series , M1: Write a program in Python Pandas to create the series.
Answer:
import pandas as pd
d={‘Term1’:45,’Term2’:65,’Term3’:24,’Term4’:89}
M1=pd.Series(d)
print(M1)
3) By using scalar value
The data can be in the form of a single value or a scalar value. But if data
is a scalar data, then the index must be provided. There can be one or more entries
in index sequence. The scalar value will be repeated to match the length of index.
Example 1):
import pandas as pd
a=pd.Series(10)
print(a)
Example 2):
import pandas as pd
a=pd.Series(10,index=[1,2,3])
print(a)
Example 3):
import pandas as pd
b=pd.Series('Yet to start',index=[5,10,15])
print(b)
Answer:
import pandas as pd
S1=pd.Series(500,index=[100,101,102,103,104])
print(S1)
Example2:
import pandas as pd
a=[1,2,3,4]
b=pd.Series(a*2)
print(b)
What will be the output of the following python code.
HOME WORK
(Consider the necessary modules imported already)
a1=numpy.arange(1,10,3)
a=pandas.Series(a1**2)
print(a)
Example1:
Output1:
import numpy as np
0 1.0
import pandas as pd 1 1.0
2 1.0
a=pd.Series(np.ones(5))
3 1.0
print(a) 4 1.0
dtype: float64
Example2:
import pandas as pd
import numpy as np
x=np.arange(1,8,2)
y=pd.Series(x)
NOTE: since input is numpy, data type is int32
print(y)
Example3:
Consider a given Series , M1: Write a program in Python Pandas to create the series.
Answer:
import pandas as pd
import numpy as np
a=np.array([45,65,24,89])
M1=pd.Series(a, index=[‘Term1’,’Term2’,’Term3’,’Term4’])
print(M1)
Adding NaN values in a Series Object:
NaN means Not a number, used to represent absence of value or
missing value
Example1:
import numpy as np
import pandas as pd
a=pd.Series([5,np.NaN,7])
print(a)
Ex1) Consider a given Series , S1: Write a program in Python Pandas to create the series.
Answer:
import pandas as pd
d={‘Eng’:34,’Hindi’:37,’Maths’:30,’Sci’:40}
S1=pd.Series(d)
print(S1)