1
import pandas as pd d=[{"year1":100,"year2":90},{"year1":90,"year2":91},{"year1":45,"year2":67},
{"year1":56,"year2":76 }] l=["store1","store2","store3","store4"]
df=pd.DataFrame(d,index=l)
print(df)
year1 year2
store1 100 90
store2 90 91
store3 45 67
store4 56 76
2.
import pandas as pd
import numpy as np
a=np.arange(1,13)
a=np.reshape(a,(4,3))
c=pd.DataFrame(a)
c=pd.DataFrame(a,columns=["first","second","third"])
print(c)
print(c.columns,c.index)
print(c.values)
print(c.shape)
First Second Third
0 1 5 9
1 2 6 10
2 3 7 11
3 4 8 12
[RangeIndex(start=0, stop=4, step=1), Index(['First', 'Second', 'Third'], dtype='object')]
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]
(4, 3)
3.
import pandas as pd
L=[] #name
LE=[] #English
LM=[] #Math
LP=[] #Physics
LC=[] #Chemistry
for i in range(6):
name=input("Enter the name:")
em=int(input("Enter English marks:"))
mm=int(input("Enter Math marks:"))
pm=int(input("Enter Physics marks:"))
cm=int(input("Enter Chemistry marks:"))
L.append(name)
LE.append(em)
LM.append(mm)
LP.append(pm)
LC.append(cm)
d={"English":LE,"Math":LM,"Physics":LP,"Chemistry":LC}
df=pd.DataFrame(d,index=L)
print(df)
print()
print("(i)")
print(df.Chemistry)
print()
print("(ii)")
print(df[['Math','English']])
print()
print("(iii)")
print(df.iloc[::2])
print()
print("(iv)")
print(df.max(axis=0))
print()
print("(v)")
df["Average"]=df.mean(axis=1)
print(df)
print()
print("(vi)")
df.sort_values("Math",ascending=False,inplace=True)
print(df["Math"].head(3))
output
Enter the name:Shalini
Enter English marks:91
Enter Math marks:57
Enter Physics marks:91
Enter Chemistry marks:91
Enter the name:Kanishk
Enter English marks:88
Enter Math marks:100
Enter Physics marks:68
Enter Chemistry marks:91
Enter the name:Viya
Enter English marks:66
Enter Math marks:99
Enter Physics marks:100
Enter Chemistry marks:99
Enter the name:Bala
Enter English marks:90
Enter Math marks:77
Enter Physics marks:50
Enter Chemistry marks:91
Enter the name:Nandit
Enter English marks:91
Enter Math marks:77
Enter Physics marks:91
Enter Chemistry marks:46
Enter the name:jithesh
Enter English marks:90
Enter Math marks:57
Enter Physics marks:35
Enter Chemistry marks:91
English Math Physics Chemistry
Shalini 91 57 91 91
Kanishk 88 100 68 91
Viya 66 99 100 99
Bala 90 77 50 91
Nandit 91 77 91 46
Jithesh 90 57 35 91
(i)
Shalini 91
Kanishk 91
Viya 99
Bala 91
Nandit 46
Jithesh 91
Name: Chemistry, dtype: int64
(ii)
Math English
Shalini 57 91
Kanishk 100 88
Viya 99 66
Balaa 77 90
Nandit 77 91
Jithesh 57 90
(iii)
English Math Physics Chemistry
Shalini 91 57 91 91
Viya 66 99 100 99
Nandit 91 77 91 46
(iv)
English 91
Math 100
Physics 100
Chemistry 99
dtype: int64
(v)
English Math Physics Chemistry Average
Shalini 91 57 91 91
Kanishk 88 100 68 91
Viya 66 99 100 99
Bala 90 77 50 91
Nandit 91 77 91 46
Jithesh 90 57 35 91
(vi)
Kanishk 100
Viya 99
Bala 76
4.
import pandas as pd
l2=[]
for i in range(1,11):
l1=[]
for j in range(1,11):
a=i*j
l1.append(a)
l2.append(l1)
df1=pd.DataFrame(l2,index=[1,2,3,4,5,6,7,8,9,10])
df1=df1.rename(columns={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J'})
print(df1)
print (df1[df1 %7==0])
print(df1.loc[4:5,'E':'F'])
del df1['D'],df1['J']
print(df1)
df1.drop(range(1,11,2),inplace=True)
print(df1)
output
A B C D E F G H I J
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
A B C D E F G H I J
1 NaN NaN NaN NaN NaN NaN 7 NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN 14 NaN NaN NaN
3 NaN NaN NaN NaN NaN NaN 21 NaN NaN NaN
4 NaN NaN NaN NaN NaN NaN 28 NaN NaN NaN
5 NaN NaN NaN NaN NaN NaN 35 NaN NaN NaN
6 NaN NaN NaN NaN NaN NaN 42 NaN NaN NaN
7 7.0 14.0 21.0 28.0 35.0 42.0 49 56.0 63.0 70.0
8 NaN NaN NaN NaN NaN NaN 56 NaN NaN NaN
9 NaN NaN NaN NaN NaN NaN 63 NaN NaN NaN
10 NaN NaN NaN NaN NaN NaN 70 NaN NaN NaN
E F
4 20 24
5 25 30
A B C E F G H I
1 1 2 3 5 6 7 8 9
2 2 4 6 10 12 14 16 18
3 3 6 9 15 18 21 24 27
4 4 8 12 20 24 28 32 36
5 5 10 15 25 30 35 40 45
6 6 12 18 30 36 42 48 54
7 7 14 21 35 42 49 56 63
8 8 16 24 40 48 56 64 72
9 9 18 27 45 54 63 72 81
10 10 20 30 50 60 70 80 90
A B C E F G H I
2 2 4 6 10 12 14 16 18
4 4 8 12 20 24 28 32 36
6 6 12 18 30 36 42 48 54
8 8 16 24 40 48 56 64 72
10 10 20 30 50 60 70 80 90
5.
import pandas as pd
s=pd.read_csv(r'C:\Users\Student\cycle2csv\Shalini.csv')
print(s)
for i,j in a.iterrows():
print(j)
6.
import pandas as pd
country=['India','Bangladesh','China','France']
capital=['New Delhi','Dhaka','Beijing','Paris']
currency=['Rupee','Taka','Yuan','Euro']
d={'Country':country,'Capital':capital,'Currency':currency}
df=pd.DataFrame(d)
print(df)
for i,j in df.iteritems():
print(j)
5
Name Class Stream Percentage
0 Aakash XII A MATH 90
1 Soham XII B COMPUTER SCIENCE 95
2 Priyanka XII C ARTS 88
3 Deepak XII D COMMERCE 80
Name Aakash
Class XII A
Stream MATH
Percentage 90
Name: 0, dtype: object
Name Soham
Class XII B
Stream COMPUTER SCIENCE
Percentage 95
Name: 1, dtype: object
Name Priyanka
Class XII C
Stream ARTS
Percentage 88
Name: 2, dtype: object
Name Deepak
Class XII D
Stream COMMERCE
Percentage 80
Name: 3, dtype: object
6
Country Capital Currency
0 India New Delhi Rupee
1 Bangladesh Dhaka Taka
2 China Beijing Yuan
3 France Paris Euro
0 India
1 Bangladesh
2 China
3 France
Name: Country, dtype: object
0 New Delhi
1 Dhaka
2 Beijing
3 Paris
Name: Capital, dtype: object
0 Rupee
1 Taka
2 Yuan
3 Euro
Name: Currency, dtype: object
7
import pandas as pd
d={"roll no":[21,22,23,24,25],"name":["amit","sourabh","abishek","ayush","pankaj"],"marks":
[592,588,595,589,583]}
df=pd.DataFrame(d)
df.to_csv(r"C:\Users\Student\Documents\Shalini.csv")
8.
import pandas as pd
df=pd.read_table((r"item.txt"))
print(df)
output
name quantity cost
0 pen 10 100
1 pencil 15 15
2 eraser 20 100