S02 Lab
S02 Lab
II BTECH II SEMESTER
Import Pandas as pd
Print (ds)
OUTPUT:
0 10
1 20
2 30
3 40
4 50
5 60
Dtype:int64
1
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
2
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
00
1 20
2 11
34
42
dtype: int
3
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
0 529
1 2925
2 5082
3 7740
4 8835
Dtype : int
4
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
5
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
6
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
7
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
8
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
SIGNATURE OF FACULTY:
9
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
Default index:
Regd no name
0 501 Deena
1 502 Chinni
2 503 Hari
10
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
INTEGER NUMBERS:
REGDNO NAME
0 501 Denna
1 502 Sra
2 503 Chinni
3 504 Hari
SIGNATURE OF FACULTY:
11
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
12
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
[0, 2, 0, 1]
SIGNATURE OF FACULTY:
14
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
15
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
SIGNATURE OF FACULTY:
17
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
18
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
2. AIM: write a pandas program to create a date from a given year, month,
day and another date from a given string formats.
SOURCE CODE:
From datetime import datetime
Date1=datetime(year=2024,month=6,day=18)
Print (“date from a given year, month, day:”)
Print(date1)
From dateutil import parser
Date2= parser. parse(14th jul,2003”)
Print(“\n date from a given string formats:”)
19
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
SIGNATURE OF FACULTY:
20
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
1) AIM: write a pandas program to split the following Data Frame into
groups based into groups based on school code. Also check the type of
Groups By object.
SOURCE CODE:
Import pandas as pd
Pd.set_option (‘display.max_rows’, None)
#pd.set_option (‘display.max_columns’, None)
Student_data=pd. Data Frame ({
‘School_code’: [‘S001’,’S002’,’S003’,’S001’,’S002’,’S004’],
‘Class’: [‘V’,’V’,’VI’,’VI’,’V’,’VI’],
‘date_of_birth’: [‘15/05/2002’,’17/05/2002’,’16/09/1999’,’25/09/1998’,
‘11/05/2002’,’15/09/1997’],
‘age’: [12,12,13,13,14,12],
‘height’: [173,192,186,167,151,159],
‘weight’: [35,32,33,30,31,32],
‘address’: [‘street1’,’street2’,’street3’,’street1’,’street2’,’street4’]},
Index = [‘S1’, ‘S2’, ‘S3’, ‘S4’, ‘S5’, ‘S6’])
Print (‘\n split the said data on school_code wise:’)
Result = student_data. groupby([‘school_code’])
For name, group in result:
Print (“\n Group:”)
21
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
Spilt the said data on school_code wise:
Group:
(‘S001’ ,)
School_code class name date_of_birth age height weight address
S1 S001 V Alberto Franco 15/05/2002 12 173 35 street1
22
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
23
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
Mean, min and max value of age form each value of the school:
Age
School_code Mean Min Max
S001 12.5 12 13
S002 13.0 12 14
S003 13.0 13 13
S004 12.0 12 12
SIGNATURE OF FACULTY:
24
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
A B C D E
0 1.000000 1.329212 -0.770033 -0.316280 0.990810
1 2.000000 -1.070816 -1.438713 0.564417 0.295722
2 3.000000 -1.626404 0.219565 0.678805 1.889273
3 4.000000 0.961538 0.104011 -0.481165 0.850229
4 5.000000 1.453425 1.057737 0.165562 0.5151018
5 6.000000 -1.336936 0.562861 1.392855 -0.063328
6 7.000000 0.121668 1.207603 -0.002040 1.627796
7 8.000000 0.354493 1.037528 -0.385684 0.519818
25
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
AIM: Write a dataframe of ten rows, four columns with random values.
write a panda program to highlight the maximum value in each column
SOURCE CODE:
Import pandas as pd
Import numpy as np
Np. random. seed (24)
Df = pd. Data Frame ({‘A’:np. linspace(1,10,10)})
Df = pd. DataFrame([df, pd.DataFrame(np.random.randn(10,4),
columns = list(‘BCDE’)),axis=1)
df.iloc[0,2] =np.nan
df.iloc[3,3] =np.nan
df.iloc[4,1] =np.nan
df.iloc[9,4] =np.nan
def highlight_max(s):
is_max =s == s.max()
return [‘background_color:green’ if v else ‘ ‘ for v in is_max]
print (“\n highlight dataframe’s specific columns:”)
df.style.apply(highlight_max,subset = pd.Index Slice[:,[‘B’, ‘C’, ‘D’, ‘E’]])
26
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
27
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
A B C D E
0 1.000000 1.329212 nan -0.316280 0.990810
1 2.000000 -1.070816 -1.438713 0.564417 0.295722
2 3.000000 -1.626404 0.219565 0.678805 1.889273
3 4.000000 0.961538 0.104011 nan 0.850229
4 5.000000 nan 1.057737 0.165562 0.5151018
5 6.000000 -1.336936 0.562861 1.392855 -0.063328
6 7.000000 0.121668 1.207603 -0.002040 1.627796
7 8.000000 0.354493 1.037528 -0.385684 0.519818
8 9.000000 1.686583 -1.325963 1.428984 -2.089354
9 10.000000 -0.129820 0.631523 -0.586538 nan
SIGNATURE OF FACULTY:
28
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
OUTPUT:
1 Lakshmi
0 2 Saranya
1 3 Bujji
2 4 Anusha
3 5 Triveni
AIM: Write a pandas program to find the sum, mean, max, min value of a
column of file.
SOURCE CODE:
Import pandas as pd
Import numpy as np
Df = pd.read_excel(‘Book2,xlsx’,engine=’openyx1’)
Print (df[‘English’].sum())
Print (df[‘Maths’].sum())
Print (df[‘Science’].sum())
Print (df[‘social’].sum())
29
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
SIGNATURE OF FACULTY:
30
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER
31