Panda Merged
Panda Merged
ipynb - Colaboratory
import pandas as pd
import numpy as np
x=np.array([1,2,3,4,5,6])
b=pd.Series(x)
print(b)
0 1
1 2
2 3
3 4
4 5
5 6
dtype: int64
import pandas as pd
import numpy as np
x=np.array([1,2,3,4,5,6])
b=pd.Series(x,index=['A','B','C','D','E','F'])
print(b)
output A
B
1
2
C 3
D 4
E 5
F 6
dtype: int64
import pandas as pd
import numpy as np
x={'Name':'Prashant','Age':17,'salary':1300}
b=pd.Series(x)
print(b)
Name Prashant
Age 17
salary 1300
dtype: object
import pandas as pd
b=pd.Series([4,-8,23,45,12])
print(b)
print(b*2)
print(b+2)
print(b**2)
print(b[b>2])
0 4
1 -8
2 23
3 45
4 12
dtype: int64
0 8
1 -16
2 46
3 90
4 24
dtype: int64
0 6
1 -6
2 25
3 47
4 14
dtype: int64
0 16
1 64
2 529
3 2025
4 144
dtype: int64
0 4
2 23
3 45
4 12
dtype: int64
https://colab.research.google.com/drive/1fD-Y5XA9gfckGYa8AwcP_nI3cg6pilxK#printMode=true 1/6
1/28/24, 5:23 PM Panda.ipynb - Colaboratory
import pandas as pd
a=pd.Series([1,2,3,4,5])
b=pd.Series([6,7,8,9,10])
c=pd.Series([11,12,13,14])
print(a+b)
print(b+c)
print(b.add(c,fill_value=0))
0 7
1 9
2 11
3 13
4 15
dtype: int64
0 17.0
1 19.0
2 21.0
3 23.0
4 NaN
dtype: float64
0 17.0
1 19.0
2 21.0
3 23.0
4 10.0
dtype: float64
import pandas as pd
import numpy as np
a=np.array([4,5,6,7,8,9,0])
p=pd.Series(a)
print(p.head())
print(p.head(3))
print(p.tail())
print(p.tail(4))
0 4
1 5
2 6
3 7
4 8
dtype: int64
0 4
1 5
2 6
dtype: int64
2 6
3 7
4 8
5 9
6 0
dtype: int64
3 7
4 8
5 9
6 0
dtype: int64
import pandas as pd
import numpy as np
a=np.array([4,5,6,7,8,9,0])
p=pd.Series(a)
print(p.loc[:2])
print(p.loc[1:4])
print(p.iloc[2:3])
0 4
1 5
2 6
dtype: int64
1 5
2 6
3 7
4 8
dtype: int64
2 6
dtype: int64
import pandas as pd
import numpy as np
s1=pd.Series([1,2,3,4,5,6,7],index=['A','B','C','D','E','F','G'])
print(s1.index)
print(s1[0:6:2])
https://colab.research.google.com/drive/1fD-Y5XA9gfckGYa8AwcP_nI3cg6pilxK#printMode=true 2/6
1/28/24, 5:23 PM Panda.ipynb - Colaboratory
import pandas as pd
import numpy as np
s2=pd.DataFrame([])
s1=pd.DataFrame([1,2,3,4,5,6,7,8,9])
print(s1)
print(s2)
0
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
Empty DataFrame
Columns: []
Index: []
import pandas as pd
Name=pd.Series(['Hardik','vatsal'])
Run=pd.Series([13,14])
dis={'Name':Name ,'Run':Run}
d=pd.DataFrame(dis)
print(d)
Name Run
0 Hardik 13
1 vatsal 14
import pandas as pd
l=[{'Name':'Dev','Age':18},
{'Name':'Prashant','Age':17},
{'Name':'Vatsal','Age':90}]
p=pd.DataFrame(l)
print(p)
Name Age
0 Dev 18
1 Prashant 17
2 Vatsal 90
import pandas as pd
l=[{'Name':'Dev','Age':18},
{'Name':'Prashant','Age':17},
{'Name':'Vatsal','Age':90}]
p=pd.DataFrame(l)
for (row_index,row_value) in p.iterrows() :
print(row_index)
print(row_value)
0
Name Dev
Age 18
Name: 0, dtype: object
1
Name Prashant
Age 17
Name: 1, dtype: object
2
Name Vatsal
Age 90
Name: 2, dtype: object
import pandas as pd
l=[{'Name':'Dev','Age':18},{'Name':'Prashant','Age':17},{'Name':'Vatsal','Age':90}]
p=pd.DataFrame(l)
for (col_index,col_value) in p.iteritems() :
print(col_index)
print(col_value)
Name
0 Dev
1 Prashant
https://colab.research.google.com/drive/1fD-Y5XA9gfckGYa8AwcP_nI3cg6pilxK#printMode=true 3/6
1/28/24, 5:23 PM Panda.ipynb - Colaboratory
2 Vatsal
Name: Name, dtype: object
Age
0 18
1 17
2 90
Name: Age, dtype: int64
<ipython-input-11-e71552355a1f>:4: FutureWarning: iteritems is deprecated and will be removed in a future version. Use .items instea
for (col_index,col_value) in p.iteritems() :
import pandas as pd
l={ 'Name':['Vatsal','Dev','Yash','Prashant'],
'Age' : [14,15,16,17],
'Score' :[100,11,39,89] }
p=pd.DataFrame(l)
print(p)
import pandas as pd
import numpy as np
s1=pd.DataFrame([1,2,3,4,5,6,7,8,9])
s1.columns=["Score"]
print(s1)
s1["List"]=20
s1["Day"]=[2,5,8,9,0,1,2,3,4]
s1["AVG"]=s1["Day"]/s1["Score"]
print(s1)
del s1["Day"]
print(s1)
s3=s1.drop("Score",axis=1)
print(s3)
s2=s1.drop(index=[2,3],axis=0)
print(s2)
Score
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
Score List Day AVG
0 1 20 2 2.000000
1 2 20 5 2.500000
2 3 20 8 2.666667
3 4 20 9 2.250000
4 5 20 0 0.000000
5 6 20 1 0.166667
6 7 20 2 0.285714
7 8 20 3 0.375000
8 9 20 4 0.444444
Score List AVG
0 1 20 2.000000
1 2 20 2.500000
2 3 20 2.666667
3 4 20 2.250000
4 5 20 0.000000
5 6 20 0.166667
6 7 20 0.285714
7 8 20 0.375000
8 9 20 0.444444
List AVG
0 20 2.000000
1 20 2.500000
2 20 2.666667
3 20 2.250000
4 20 0.000000
5 20 0.166667
6 20 0.285714
7 20 0.375000
8 20 0.444444
Score List AVG
0 1 20 2.000000
1 2 20 2.500000
4 5 20 0.000000
5 6 20 0.166667
6 7 20 0.285714
https://colab.research.google.com/drive/1fD-Y5XA9gfckGYa8AwcP_nI3cg6pilxK#printMode=true 4/6
1/28/24, 5:23 PM Panda.ipynb - Colaboratory
7 8 20 0.375000
8 9 20 0.444444
import pandas as pd
d={'a':{'Name':'Prashant','Age':17,'score':1},
'b':{'Name':'Vatsal','Age':18,'score':2},
'c':{'Name':'Dev','Age':19,'score':3}
}
df=pd.DataFrame(d)
print(df)
print(df.loc[:,'a':'b'])
print(df.iloc[:,0:3])
a b c
Name Prashant Vatsal Dev
Age 17 18 19
score 1 2 3
a b
Name Prashant Vatsal
Age 17 18
score 1 2
a b c
Name Prashant Vatsal Dev
Age 17 18 19
score 1 2 3
import pandas as pd
d={'a':{'Name':'Prashant','Age':17,'score':1,'salary':1200},
'b':{'Name':'Vatsal','Age':18,'score':2,'salary':1100},
'c':{'Name':'Dev','Age':19,'score':3,'salary':1000},
'd':{'Name':'Yash','Age':20,'score':4,'salary':1050},
'd':{'Name':'ketan','Age':16,'score':3,'salary':1150}
}
df=pd.DataFrame(d)
print(df)
print(df.head())
print(df.tail())
print(df.head(2))
print(df.tail(3))
print(df[1:3])
a b c d
Name Prashant Vatsal Dev ketan
Age 17 18 19 16
score 1 2 3 3
salary 1200 1100 1000 1150
a b c d
Name Prashant Vatsal Dev ketan
Age 17 18 19 16
score 1 2 3 3
salary 1200 1100 1000 1150
a b c d
Name Prashant Vatsal Dev ketan
Age 17 18 19 16
score 1 2 3 3
salary 1200 1100 1000 1150
a b c d
Name Prashant Vatsal Dev ketan
Age 17 18 19 16
a b c d
Age 17 18 19 16
score 1 2 3 3
salary 1200 1100 1000 1150
a b c d
Age 17 18 19 16
score 1 2 3 3
import pandas as pd
s1={'id':[1,2,3,4,5],'value1':[12,13,14,15,16],'value2':[10,20,30,40,50]}
s2={'id':[6,7,8,9,0],'value1':[11,22,33,44,55],'value2':[100,200,300,400,500]}
dif1=pd.DataFrame(s1)
dif2=pd.DataFrame(s2)
marge={'Data1':dif1,'Data2':dif2}
dif4=pd.concat(marge)
print(dif4)
dif3=pd.concat([dif1,dif2],ignore_index=True)
print(dif3)
dif5=pd.concat([dif1,dif2],axis=1)
print(dif5)
id value1 value2
Data1 0 1 12 10
1 2 13 20
2 3 14 30
https://colab.research.google.com/drive/1fD-Y5XA9gfckGYa8AwcP_nI3cg6pilxK#printMode=true 5/6
1/28/24, 5:23 PM Panda.ipynb - Colaboratory
3 4 15 40
4 5 16 50
Data2 0 6 11 100
1 7 22 200
2 8 33 300
3 9 44 400
4 0 55 500
id value1 value2
0 1 12 10
1 2 13 20
2 3 14 30
3 4 15 40
4 5 16 50
5 6 11 100
6 7 22 200
7 8 33 300
8 9 44 400
9 0 55 500
id value1 value2 id value1 value2
0 1 12 10 6 11 100
1 2 13 20 7 22 200
2 3 14 30 8 33 300
3 4 15 40 9 44 400
4 5 16 50 0 55 500
https://colab.research.google.com/drive/1fD-Y5XA9gfckGYa8AwcP_nI3cg6pilxK#printMode=true 6/6
1/28/24, 5:22 PM Basicofpython.ipynb - Colaboratory
a=int(input("Enter a: "))
b=int(input("Enter b: "))
print(a+b)
str1="Hello"
print(type(str1))
str2='Python'
str3='''Program'''
print(str1,str2,str3)
str1="hello world"
print(str1)
str2=str1+str3+"App"
print(str2)
list1=['hi','hello','Prashant']
print(list1)
print(list1[1:3])
list1.pop(1)
print(list1)
list1.remove('hi')
print(list1)
del list1[0]
print(list1)
list1.clear()
print(list1)
list2=['hi','hello','best','banna','apple']
print(list2)
list2[2:4]=['cheir','Prashant']
print(list2)
list2.append('kiwi')
print(list2)
list2.insert(4,'clear')
print(list2)
t=()
print(t)
t=('a','b','c','d','e')
print(t)
l=list(t)
print(l)
dis={}
print(dis)
dis={'name':"john",'age':15,'dep':"hello"}
print(dis)
print(dis['name'])
print(dis['dep'])
a=[1,2,3],[2,4,5],[4,5,6]
b=[-1,-2,-3],[-2,-4,-5],[-4,-5,-6]
print(a+b)
x=6
y=5
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x**y)
print(x//y)
#comparision
x=5
y=9
print(x==y)
print(x!=y)
print(x>y)
print(x<y)
print(x>=y)
print(x<=y)
#assignment Oprators
a=5
b=9
x=a
i t( )
https://colab.research.google.com/drive/1BtpsMG1LGR_k-UZvAyX0gD2m0L5N_J6s?usp=chrome_ntp#printMode=true 1/4
1/28/24, 5:22 PM Basicofpython.ipynb - Colaboratory
print(x)
x+=b
print(x)
x-=b
print(x)
x*=b
print(x)
x/=b
print(x)
x%=b
print(x)
x**=b
print(x)
x//=b
print(x)
#Bitwise Oprators
p=3
q=7
print(p&q)
print(p|q)
print(p^q)
print(~p)
p<<=p
print(p)
p>>=p
print(p)
p=1
q=0
s=p and q
print(s)
s=p or q
print(s)
s= not q
print(s)
#membership
a=[1,2,3,4,5,6]
print(1 in a)
print(2 not in a)
#Identity
a=[1,2,3,4,5,6]
b=[2,4,5,7]
print(a is b)
print(a is not b)
output Enter a: 44
Enter b: 55
99
<class 'str'>
Hello Python Program
hello world
hello worldProgramApp
['hi', 'hello', 'Prashant']
['hello', 'Prashant']
['hi', 'Prashant']
['Prashant']
[]
[]
['hi', 'hello', 'best', 'banna', 'apple']
['hi', 'hello', 'cheir', 'Prashant', 'apple']
['hi', 'hello', 'cheir', 'Prashant', 'apple', 'kiwi']
['hi', 'hello', 'cheir', 'Prashant', 'clear', 'apple', 'kiwi']
()
('a', 'b', 'c', 'd', 'e')
['a', 'b', 'c', 'd', 'e']
{}
{'name': 'john', 'age': 15, 'dep': 'hello'}
john
hello
([1, 2, 3], [2, 4, 5], [4, 5, 6], [-1, -2, -3], [-2, -4, -5], [-4, -5, -6])
11
1
30
1.2
1
7776
1
False
True
False
True
https://colab.research.google.com/drive/1BtpsMG1LGR_k-UZvAyX0gD2m0L5N_J6s?usp=chrome_ntp#printMode=true 2/4
1/28/24, 5:22 PM Basicofpython.ipynb - Colaboratory
False
True
5
14
5
45
5.0
5.0
1953125.0
217013.0
3
7
4
-4
24
0
0
1
True
True
False
False
amount=2000
if amount<1000:
discount=amount*0.05
print ("Discount",discount)
elif amount<5000:
discount=amount*0.10
print ("Discount",discount)
else:
discount=amount*0.15
print ("Discount",discount)
i = 0
while i < 9:
print ('The count is:', i)
i=i+1
for i in "Python":
print(i)
Discount 200.0
Net payable: 1800.0
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
P
y
t
h
o
n
def sum(a,b):
print(a+b)
sum(55,44)
99
https://colab.research.google.com/drive/1BtpsMG1LGR_k-UZvAyX0gD2m0L5N_J6s?usp=chrome_ntp#printMode=true 3/4
1/28/24, 5:22 PM Basicofpython.ipynb - Colaboratory
https://colab.research.google.com/drive/1BtpsMG1LGR_k-UZvAyX0gD2m0L5N_J6s?usp=chrome_ntp#printMode=true 4/4
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
import numpy as np
a=np.array([1,2,3,4])
print(a)
b=np.array([1,2.3,4,5])
print(b)
a1=np.array([[1,2],[1,2]])
print(a1)
print(a.shape)
print(a.ndim)
print(a.dtype.name)
print(a.itemsize)
print(a.size)
print(type(a))
print(np.ndarray)
[1 2 3 4]
[1. 2.3 4. 5. ]
[[1 2]
[1 2]]
(4,)
1
int64
8
4
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
import numpy as np
e=np.ones(5)
f=np.zeros(5)
print(e,f)
a=np.array([1,2,3,4])
a=a+5
print(a)
a=a-5
print(a)
a=a*5
print(a)
a=a/5
print(a)
b=np.array([2,3,4,5,6])
h=b
b=b+4
print(b)
print(h)
k=np.copy(b)
k[0]=0
print(k)
print(b)
a=np.linspace(1,10,6)
print(a)
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 1/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
import numpy as np
b=np.arange(10) # 0 to 9
print(b)
c=np.arange(5,10)
d=np.arange(10,1,-1)
print(c)
print(d)
[0 1 2 3 4 5 6 7 8 9]
[5 6 7 8 9]
[10 9 8 7 6 5 4 3 2]
import numpy as np
a=np.array([1,2,3,4])
b=np.array([5,6,7,8])
c=np.concatenate([a,b])
print(c)
[1 2 3 4 5 6 7 8]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
print(a)
a=a+4 #+,-,*,/
print(a)
print(a.ndim)
print(a.shape)
print(a.size)
print(a.itemsize)
print(a.reshape(8,1))
print(a.reshape(2,4))
[[1 2 3 4]
[2 3 4 5]]
[[5 6 7 8]
[6 7 8 9]]
2
(2, 4)
8
8
[[5]
[6]
[7]
[8]
[6]
[7]
[8]
[9]]
[[5 6 7 8]
[6 7 8 9]]
b=np.empty([2,3])
print(b)
b=np.empty([3,3],dtype=int)
print(b)
b=np.empty([1,3],dtype=float)
print(b)
[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]
[[ 4607182418800017408 0 0]
[-9223372036854775808 -4616189618054758400 -9223372036854775808]
[ 0 0 4607182418800017408]]
[[0. 0.99999869 1. ]]
import numpy as np
a=np.array([1,2,3,4,5,6,7,8])
b=np.array_split(a,[3,5])
print(b)
c=np.array([[1,2,3,4],[2,3,4,5]])
d=np.array_split(c,4)
print(d)
sp=np.hsplit(c,2)
print(sp)
sp=np.vsplit(c,2)
print(sp)
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 2/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5],[4,5,6,7]])
print(a[0:2,:])
print(a[:,1:3])
b=np.array([1,2,3,4,5,6]) #indexing
print(b[2:-2])
print(b[0:1])
print(b[0:5:2])
c=np.zeros([2,2],dtype=int)
print(c)
c=np.zeros([3,2],dtype=float)
print(c)
d=np.eye(4)
print(d)
[[1 2 3 4]
[2 3 4 5]]
[[2 3]
[3 4]
[5 6]]
[3 4]
[1]
[1 3 5]
[[0 0]
[0 0]]
[[0. 0.]
[0. 0.]
[0. 0.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
A=np.ones([2,2],dtype=int)
print(A)
A=np.ones([3,1],dtype=float)
print(A)
[[1 1]
[1 1]]
[[1.]
[1.]
[1.]]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
b=np.empty([3,4],dtype=int)
print(b)
[[93953841706394 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
d=np.concatenate([a,a],axis=0)
print(d)
x=np.array([1,2,3,4,5,6])
b=np.hstack(x)
c=np.vstack(x)
print(b)
print(c)
[[1 2 3 4]
[2 3 4 5]
[1 2 3 4]
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 3/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
[2 3 4 5]]
[1 2 3 4 5 6]
[[1]
[2]
[3]
[4]
[5]
[6]]
import numpy as np
a=np.array([[1,2,3,4],[2,3,4,5]])
a=a.reshape(4,2)
print(a)
a=([1,2,3,4,5])
b=([6,7,7,8,5])
c=np.hstack((a,b)) #np.vsplit and np.hsplit
d=np.vstack((a,b))
print(c)
print(d)
[[1 2]
[3 4]
[2 3]
[4 5]]
[1 2 3 4 5 6 7 7 8 5]
[[1 2 3 4 5]
[6 7 7 8 5]]
x=([1,2,3])
y=([2,4,6])
plt.plot(x,y)
plt.xlabel("Score")
plt.ylabel("Match")
plt.title("Day-1")
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 4/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
x = [0,2,3,4,5,6,7]
y1=[5,7,9,0,-1,6,8]
y2=[0,6,-2,-9,3,1,9]
plt.plot(x,y1,label="Day",linewidth=4,linestyle='dashed')
plt.plot(x,y2,label="Score")
plt.legend(loc=2)
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 5/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
x=np.arange(1,11)
y=x*3-2
plt.plot(x,y,marker='+',markeredgecolor='red',markersize='10')
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 6/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
a=np.array([3,6,9,1])
b=np.array([9,8,3,3])
plt.bar(a,b,width=0.6,color='pink')
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 7/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
city=np.array(['Morbi','Rajkot','Halvad','Delhi'])
temp=np.array([43,34,29,45])
plt.barh(city,temp)
plt.show()
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 8/9
1/28/24, 5:22 PM Untitled0.ipynb - Colaboratory
https://colab.research.google.com/drive/13dAeT5w4IADkZ1LART9AxGEls6qfzKW5#printMode=true 9/9