0% found this document useful (0 votes)
7 views31 pages

S02 Lab

This document outlines various exercises for a skill-oriented course on Python-Pandas, including creating and manipulating Series and DataFrames. It includes source code examples for operations such as addition, subtraction, multiplication, and division of Series, as well as DataFrame creation, modification, and string manipulation. The document provides both the source code and expected output for each exercise, serving as a practical guide for students learning Pandas.

Uploaded by

saripudisaranya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views31 pages

S02 Lab

This document outlines various exercises for a skill-oriented course on Python-Pandas, including creating and manipulating Series and DataFrames. It includes source code examples for operations such as addition, subtraction, multiplication, and division of Series, as well as DataFrame creation, modification, and string manipulation. The document provides both the source code and expected output for each exercise, serving as a practical guide for students learning Pandas.

Uploaded by

saripudisaranya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

DEPARTMENT OF COMPUTER SCIENCE

II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


EXERCISE-1
1.AIM: write a Pandas program to create and delay a one- dimensional
array-like object containing an array of and using Pandas module.
SOURCE CODE:

Import Pandas as pd

Ds=Pd. Series ([10,20,30,40,50,60])

Print (ds)

OUTPUT:

0 10

1 20

2 30

3 40

4 50

5 60

Dtype:int64

2. AIM: Write a Pandas Program to convert a panda module Series to


python list and it's type.
SOURCE CODE:
import Pandas as pd
ds:=Pd. Series ([25, 35, 45, 55, 65])
Print (ds)
Print (ds.tolist())
OUTPUT:
0 25
1 35
2 45
3 55

1
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


4 65
Dtype:int64
[25,35,45,55,65]
Python list and it's type :
import pandas as pd
ds=Pd. Series ([25, 35, 45,55,65])
Print (ds)
Print (ds-tolist())
Print(type(ds.tolist())
OUTPUT:
0 25
1 35
2 45
3 55
4 65
Dtype:int64
[25,35,45,55,65]
<’class list’>

3. AIM: write a Pandas Program to add two pandas Series.


SOURCE CODE:
import Pandas as pd
S1=pd. Series([23, 45, 67,90,80])
S2=pd. Series([23,65,76,86,93])
S=s1+s2
Print (s)

2
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


OUTPUT:
0 46
1 110
2 143
3 176
4 182

AIM: write a program to subtract two pandas Series.


SOURCE CODE:
import pandas as pd
a=Pd. series([23,65,71,90,953])
b= Pd. Series ([23, 45, 66,86,923])
C=a-b
Print (C)

OUTPUT:
00
1 20
2 11
34
42
dtype: int

3
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


AIM: write a program to multiply two pandas series.
SOURCE CODE:
import pandas as pd
P=Pd-Series ([23,65,77,90,95])
q=Pd-Series ([23,45, 6686,93])
r=p*q
Print (r)

OUTPUT:
0 529
1 2925
2 5082
3 7740
4 8835
Dtype : int

AIM: write a program to divide two pandas series.


SOURCE CODE:
import Pandas as pd
d=Pd-Series ([23,65,77,90,95])
e=Pd-Series ([23,45, 66,86,93])
f=d%e
Print (f)

4
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


OUTPUT:
00
1 20
2 11
34
42
Dtype:int

4 AIM: write a Pandas program to convert a Numpy array to a pandas


Series.
SOURCE CODE:
import numpy as np
import Pandas as pd
a = np. array([5,7,6,9,4])
Print (a)
b= Pd. Series(a)
Print(b)
OUTPUT:
[5 7 6 9 4]
05
17
26
39
44
Dtype: int64
SIGNATURE OF FACULTY:

5
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


B) PANDAS DATAFRAMES:
AIM: Write a pandas program to create and display a dataframe from a
specified dictionary data which has the index labels.
SOURCE CODE:
Import pandas as pd
Exam_data={‘names’:['Anastasia', 'Dima, "Katherine, James, Emily, Michael,
Matthew', 'Laura, 'Kevin, Jonas'),
‘Score’: [12-5,9,16-5, nр, nan, 9, 20, 14.5, np.nan, 8,19),
‘attempts’ : [1,3,2,3,2,3,1,1,2,1),
‘qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes' 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', ‘h’, ‘I’, ‘j’]
df=pd_DataFrame (exam_data, index= labels)
Print (df)
OUTPUT:
Names Score attempts qualify
A Anastisa 12.5 1 Yes
B Dima 9 3 No
C Katherine 16.5 2 Yes
D James Nan 3 No
E Emily 9 2 No
F Michacel 20 3 Yes
G Matthew 14.5 1 Yes
H Laura Nan 1 No
I Kevin 8 2 No
J Jonas 19 1 Yes

6
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


AIM: write a Pandas Program to change the James to Suresh in name
column of the Dataframe.
SOURCE CODE:
import Pandas as pd
import numpy as np
Exam data = {'names: ['Anastasia, 'Dima", "Katherine, James Emily, Michacel,
"Matthew, 'Laura', 'Kevir, Jonas'],
'Score’: [12.5, 9, 16.5, 56,9,20,14.5,65,8,19],
‘attempts’:[1,3,2,3,2,3,1,1,2,1],
‘qualify’:[‘yes’, ‘no’, ‘yes’, ‘no’, ’no’, ‘yes’, ‘yes’, ‘no’, ‘no’, ’yes’]}
‘labels’=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘I’, ‘j’]
Df = DataFrame (exam_data, index=labels)
Df[‘name’]=df[‘name’].replace(‘James’, ‘Suresh’)
Print(df)
OUTPUT:
Names Score attempts Qualify
A Anastasia 12.5 1 Yes
B Dima 9 3 No
C Katherine 16.5 2 Yes
D Suresh 56.0 3 No
E Emily 9.0 2 No
F Michacel 20.0 3 Yes
G Matthew 14.5 1 Yes
H Laura 65.0 1 no
I Kevin 8.0 2 no
J Jonas 19.0 1 Yes

7
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


3. AIM: write a Pandas program to insert a new column in Existing Data
Frame.
SOURCE CODE:
import Pandas as Pd
import NumPy as np
exam _data = { 'home': ['Anastasia’, 'Dima', 'Katherine’, ‘James’, Emily',
'Michacel’, ‘Laura', 'Kevin', ‘Jonas', 'Sra'],
‘Score’: (12.5, 9,16.5, 56, 9, 20, 14.5,65,4,19],
'attempts’: [1,3,2,3,2,3,1,1,2, 1],
'quality': ['yes', 'no', 'yes’, 'no', 'no, 'yes', 'yes' no, 'no' ‘yes’]}
labels = ['a', 'b', 'c', 'd', 'e', 'g’, ‘h’, ‘ I’ ,’ j']
df= Pd. DataFrame (exam-data, index=labels)
Subject = ['flat', 'Dbms', 'Mefa', 'Java', 'Python’, ‘chemistry’, 'Maths’, 'social,
Telugu, ‘Science’)
df ['subject) = Subject
Print ("\new Dataframe after inserting the 'subject' Column")
Print(df)
OUTPUT:
Name Score attempts Qualify
A Anastasia 12.5 1 Yes
B Dima 9.0 3 No
C Katherine 16.5 2 Yes
D James 56.0 3 No
E Emily 9.0 2 No
F Michacel 20.0 3 Yes
G Matthew 14.5 1 Yes
H Laura 65.0 1 No
I Kevin 8.0 2 No
J Jonas 19.0 1 Yes

8
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


4. AIM: write a pandas Program to get list from DataFrame Column
headers.
SOURCE CODE:
import pandas as pd
import Numpy as np
Exam_data=['name': ['Anastasia’, 'Dima, 'Katherine’, ‘James’, ’Emily’,
‘Michacel’, ‘Laura’, ‘Kevin’, ’Jonas’, ‘Sra’],
'Score’: [12.5,9, 16.5, 56, 9, 20, 14-5,65,8,19],
‘attempts’: [1, 3, 2,3,2,3, 1, 1,2,1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes' 'yes’, 'no', 'no, 'yes']}
labels: ['a', 'b', ‘c', ‘d', 'e', 'f', 'g’, ‘I’, ’h’,’ I’, ‘j’]
df = pd.Dataframe (exam-data, index = labels)
Print (list (df. Columns. Values))
OUTPUT:
[‘name’, ‘score’, ‘attempts’, ‘qualify’]

SIGNATURE OF FACULTY:

9
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


C)PANDAS INDEX:
AIM: write a pandas program to display the default index and set a column
as an index in a given dataframe.
SOURCE CODE:
Import Pandas as pd
data=['Regdno': [‘501’, ‘502’, ‘503’],
'name': ['Deena, ’chinni’, ‘’Hari’]}
df = Pd. Dataframe(data)
Print("Default index:”)
Print (df)

OUTPUT:
Default index:
Regd no name
0 501 Deena
1 502 Chinni
2 503 Hari

10
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


2) AIM: write a Pandas program to Create an index labels by using 64-bit
integer, using floating -point numbers In a given data frame.
SOURCE CODE:
import pandas as pd
data= { 'regdno': ['501', '502', '503’, '504'],
'name': ['Deena’, 'Sra’, ‘Chinni’, ‘Hari’)]
df= Pd DataFrame (data)
Print("Integer numbers:")
Print (df)
Df1=Pd. Dataframe(data, index= [1.0,2.0,3.0,4.0])
Print ("floating index)
Print (df1)

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


D) PANDAS STRING AND REGULAR EXPRESSIONS:
AIM: write a Pandas Program to covert all the String values to upper,
lower cases in a given pandas Series. Also find the length of the string
Values.
SOURCE CODE:
Ds= Pd-series([ 'Apple', 'Banana', 'mango', 'Strawberry))
Print (ds.str.len())
Print (ds.str.lower())
Print (dS.str.supper())
OUTPUT:
05
16
25
3 10
Dtype:int64
0 Apple
1 Banana
2 Mango
3 Strawberry
Dtype: object
0 APPLE
1 BANANA
2 MANGO
2 STRAWBERRY

12
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


2. AIM: write a pandas program to remove Whitespaces, left sided
whitespaces and right sided whitespaces of the string valves of a given
pandas series.
SOURCE CODE:
Ds= pd-series(['Apple ', 'Banana ',’ mango ‘ , 'strawberry '])
Print (ds.str. strip())
Print (ds-. Str.rstrip())
Print(ds.str. lstrip())
OUTPUT:
0 Apple
1 Banana
2 Mango
3 Strawberry
Dtype: object
0 Apple
1 Banana
2 Mango
3 Strawberry
Dtype: object
0 Apple
1 Banana
2 mango
3 Strawberry
Dtype: object

3 AIM: Write a pandas program to count of occurrence of a Specified


Substring in a Dataframe Column.
A lambda function is a Small anonymous function.
Syntax:
lambda arguments: expression.
13
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


X= lambda a: a+10
Print (x(5))
SOURCE CODE:
data= { 'name': ['mangoes', 'apples', 'Strawberry, grapes']}
df = Pd. Data Frame (data)
df= list (map(lambda x: x.count("P"), df [name]))
Print (df)

OUTPUT:
[0, 2, 0, 1]

4. AIM: write a Pandas program to swap the cases of a specified character


column in a given Dataframe.
SOURCE CODE:
data= {'name': ['mangoes', 'apples', 'strawberry, 'grapes']}
df=Pd-Data frame (data)
df ['swap']=list (map(lambda x: X.swapcase(), df ['name']))
Print (df)
OUTPUT:
NAME SWAP
0 mangoes MANGOES
1 apples APPLES
2 strawberry STARWBERRY
3 grapes GRAPES

SIGNATURE OF FACULTY:

14
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


E) PANDAS JOINING AND MERGING DATAFRAME:
1) AIM: Write a pandas program to join the two join the two given
dataframe along rows and columns and assign all data.
SOURCE CODE:
impost pandas as pd
d1={'Name': ['fancy', 'Dinna', ‘sara’, ‘Frize'],
‘Address’: ['Guntur’, ‘Amaravathi’, ‘Tenali! "Hyderabad]}
Df1= Pd-Dataframe (d1)
D2={'Name:' Chinnu’, ‘Anusha’, ‘Triveni’, ‘Sirisha],
Address: [ ‘Hyderabad’, ‘Guntur’, ‘Banglore’, ‘Usa]}
Df2= Pd-Dataframe (da)
X=Pd. concat ([df1,df2])
Print(x)
OUTPUT:
NAMES ADDRESS
0 fancy Guntur
1 Dinna Amaravathi
2 Sara Tenali
3 Frize Hyderabad
0 Chinnu Hyderabad
1 Anusha Guntur
2 Triveni Banglore
3 Sirisha USA

15
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


2. AIM: write a pandas Program to append a list of dictionaries or series to
an Existing Data frame and display the Combined data.
SOURCE CODE:
import Pandas as pd
d1 = {'Name': ['fancy’, ‘Dinna’, ‘Sara', 'Frize’],
Address: ['Guntur’,’ Amaravathi’, ‘Tenali’, 'Hyderabad']}
Df1=Pd. Data frame (d)
new= {'Name': 'Chinnari’, 'Address': 'Hyderabad"}
df1 = df1. append (new, ignore-index=True)
Print (df)
OUTPUT:
Name Address
0 Fancy Guntur
1 Dinna Amaravathi
2 Sara Tenali
3 Frise Hyderabad
4 Chinnari Hyderabad

3) AIM: write a pandas program to join the two dataframes with


matching records from both sides where available.
SOURCE CODE:
D1={‘name’:[‘Saru’, ’Bujji’, ’Anusha],
‘no’:[23,45,65]}
Df1=pd dataframe(d1)
D2={‘name’:[‘Vincy’,’ Ahalya’, ’Lakshmi’],
‘no’:[25,34,37]}
Df2=pd.dataframe(d2)
16
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


Df3=pd.merge(df1,df2,on=’NO’, how=’outer’)
Df4=pd.merge(df1,df2,on=’NO’, how=’inner’)
Df5= pd. merge (df1,df2,on=’NO’, how=’right’)
Df6= pd. merge (df1,df2,on=’NO’, how=’left’)
Print (df3,’\n’, df4,’\n’, df5,’\n’, df6,’\n’)
OUTPUT:
Name_x no Name_y
0 Saru 23 Vincy
1 Bujji 45 Ahalya
2 Anusha 65 Lakshmi
Name_x no Name_y
0 Saru 23 Vincy
1 Bujji 45 Ahalya
2 Anusha 65 Lakshmi
Name_x no Name_y
0 Saru 23 Vincy
1 Bujji 45 Ahalya
2 Anusha 65 Lakshmi
Name_x no Name_y
0 Saru 23 Vincy
1 Bujji 45 Ahalya
2 Anusha 65 Lakshmi

SIGNATURE OF FACULTY:

17
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


F) PANDAS TIME SERIES:
1AIM: write a pandas program to create
a) Datetime object for Jan 15 2012
b) specific date and time of 9:20pm
c) local date and time
d) A date without time
e) current time
f) Time from a date time
g) current local time
SOURCE CODE:
From datetime import datetime
Print (“Datetime object for Jan 15, 2012, is”)
Print (Datetime (2012,1,15))
Print (“\n specific date and time of 9:20 pm”)
Print (“datetime (2012,1,11,21,20))
Print (“\n local date and time”)
Print (Datetime.now ())
Print (“\n data without time:”)
Print (Datetime.data(datetime (2012,1,15,21,20))
Print (“\n current date”)
Print (Datetime.now ().data ())
Print (“\n time from a datetime:”)
Print (datetime. time (datetime (2012,1,15,18,12)))
Print (“\n current local time:”)
Print (datetime.now (). date))

18
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


OUTPUT:
Datetime object for JAN15 2012 is
2012-01-15 00:00:00
Specific date and time of 9:20pm
2012-01-11 21:20:00
Local date and time
2024-02-15 14:57:51480546
A date without time
2012-01-15
Current date
2024-02-15
Time from a datetime:
18:12:00
Current local time:
2024-02-15.

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


Print(date2)

OUTPUT:

Date from a given year, time, month, day:


2024-06-18 00:00:00
Date from a given string formats:
2003-07-14 00:00:00

SIGNATURE OF FACULTY:

20
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


G) PANDAS GROUPING AGGREGATE:
Consider Dataset
School CLASS NAME Date_of_birth AGE HEIGHT WEIGHT ADDRESS
S1 S001 V Alberto Franco 15/05/2002 12 173 35 street1
S2 S002 V Gino Mcneil 17/05/2002 12 192 32 street2
S3 S003 VI Ryan Parkes 16/02/1999 13 186 33 street3
S4 S004 VI Eesha Hinton 25/09/1998 13 167 30 street1
S5 S005 V Gino Mcneil 11/05/2002 14 151 31 street2
S6 S006 VI David Parkes 15/09/1997 12 159 32 street4

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


Print(name)
Print(group)
Print(“\n type of the object:”)
Print(type(result))

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

S4 S001 VI Eesha Hinton 25/09/1998 13 167 30 street1

Type of the object:


<class ‘pandas.core.groupby.generic.DataFrame Groupby’>
Group:
(‘S002’, )
School_code class name date_of_birth age height weight address

S2 S002 V Gino Mcneill 17/05/2002 12 192 32 street2

S5 S002 V Gino Mcneill 11/05/2002 14 151 31 street2

Type of the object:


<class ‘pandas.core.groupbygeneric.DataFrame groupby’>
Group:
(‘S003’,)
school_code class name date_of_birth age height weight address

S3 S003 VI Ryan Parkes 16/02/1999 13 186 33 street3

Type of the object:


< class ‘pandas.core.groupbygeneric.DataFrame groupby’>

22
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


Group:
School_code class name date_of_birth age height weight address

S6 S004 VI David Parkes 15/09/1997 12 159 32 Steet4

Type of the object:


< class ‘pandas.core.groupbygeneric.DataFrame groupby’>

2.AIM: Write a pandas program to split the following dataframe by school


code and get mean ,min and value of age for each school.
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 mean, min and max value of age for each value of the school:’)
Grouped_single =
student_data.groupby(‘school_code’).agg({‘age’:[‘mean’,’min’,’max’]})
Print (grouped_single)

23
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


H) PANDAS STYLING
AIM: Write a dataframe of tenrows, four columns with random values .
write a pandas program to highlight the negative numbers red and positive
numbers black.
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)
Def color_negative _red(val)
Color = ‘red’ if val < 0 else ‘black’
return ‘color: %s’ % color
print (“\n negative numbers red and positive numbers black:”)
df. style. applymap(color_negative_red)

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


8 9.000000 1.686583 -1.325963 1.428984 -2.089354
9 10.000000 -0.129820 0.631523 -0.586538 0.290720

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


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

AIM: Write a dataframe of tenrows, four columns with random values.


write a pandas program to highlight dataframe specific columns.
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

27
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


def. highlight_cols(s):
color = ‘YELLOW’
return ‘background_color:%s’ % color
print (“\n highlight specific columns:”)
df.style.applymap(highlight_cols,subset=pd.indexSlice[:’B’,’C’]])

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


I)EXCEL
AIM: Write a pandas program to import excel data into a pandas
dataframe.
SOURCE CODE:
Import pandas as pd
Import numpy as np
Df = pd. read_excel(‘Book.xlsx’,engine=’openpyx1’)
Print(df)

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

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)


OUTPUT:
50
5.0
8
2

SIGNATURE OF FACULTY:

30
DEPARTMENT OF COMPUTER SCIENCE
II BTECH II SEMESTER

SKILL ORIENTED -II( APPLICATIONS OF PYTHON-PANDAS)

31

You might also like