0% found this document useful (0 votes)
8 views

Lab Programmes Adwaith

Uploaded by

edwinbaby2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab Programmes Adwaith

Uploaded by

edwinbaby2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PROGRAM1:

AIM:Create a Series from a dictionary and ndarray.

CODE

OUTPUT;
a 1
b 2
c 3
dtype: int64
0 1
1 2
2 3
dtype: int32
PROGRAM 2:
AIM: Create a Pandas Series to perform all mathematical operations

CODE

OUTPUT;
Substraction:
1 -10
2 -10
3 -10
4 -10

Multiplication: Power;
1 231 1 3105570700629903195
2 264 2 5729018530666381312
3 299 3 -4649523274362944347
4 336 4 -1849127232522420224
dtype: int64 dtype: int64

Divison:
1 0.523810
2 0.545455
3 0.565217
4 0.583333
dtype: float64
PROGRAM 3
AIM:Create a pandas series from scalar values given by user.

CODE;

OUTPUT:
Enter the scalar value to create Series:5
Enter the number of indices in Series:6
Created Series is:
0 5
1 5
2 5
3 5
4 5
5 5
dtype: int64

Program 4:
AIM: Create a series to store the amount of sales made by a salesperson for
the last year(whole months).
a) Display the sales amount which is greater than 10000
b) Display the sales amount in the first four months
c) Display the sales amount in the last four months.

CODE;

Sales Amount > 10000:


Feb 15000
Mar 45000
Apr 30000
May 20000
Jun 35000
Jul 15000
Aug 75000
Sep 45000
Nov 65000
Dec 54000
dtype: int64
Sales Amount in First Four Months: Sales Amount for last four months
Jan 2000 Sep 45000
Feb 15000 Oct 5000
Mar 45000 Nov 65000
Apr 30000 Dec 54000
dtype: int64 dtype:int64

PROGRAM 5

AIM: Create a DataFrame creation using dictionary with given data:

2015:[256,452,635,965],2016:[256,452,635,696],
2017:[452,474,725,854],2018:[1021,958,528,425]

Index=[‘QTR1’,’QTR2’,’QTR3’,’QTR4’]
Find the following:

I) Display the data frame


Ii) Sales of 2017
Iii) Sales of quarter 2
Iv) Sales in 2015,2016,2017 for quarter 1&2
V) Indices of data frame and columns of data frame
Vi) Row and column labels
Vii) Number of rows and columns
Viii)Sales in qtr 4 during the year 2018
Ix) Change sales in qtr3 during 2017 to 754 and print
X) Add a new column 2019 with values [690,813,217,721] for all quarters

CODE:
import pandas as pd
sales=pd.DataFrame({'2015':[256,452,635,965], '2016':[745,785,478,547], '2017':
[452,474,725,854],
'2018':[1021,958,528,425]}, index=['QTR1','QTR2','QTR3','QTR4'])
#i
print('DataFrame')
print(sales)
#ii
print('Sales of 2017')
print(sales['2017'])
#iii
print('Sales in Quarter 2')
print(sales.loc['QTR2'])
#iv
print('Sales in 2015, 2016, 2017 for Quarter 1 and 2')
print(sales.loc['QTR1':'QTR2','2015':'2017'])
#v
print('Index of DataFrame')
print(sales.index)
print('Columns of DataFrame')
print(sales.columns)
#vi
print('Row and Column Labels')
print(sales.axes)
#vii
print('Number of Rows and Columns')
print(sales.shape)
#viii
print('Sales of Quarter 4 During the Year 2018')
print(sales.loc['QTR4','2018'])
#ix
print('Changing Sales in QTR3 During 2017 to 754')
sales.loc['QTR3','2017']=754
print(sales)
#x
print('Add a New Column 2019 With Values')
sales['2019']=[524,639,785,458]
print(sales)

OUTPUT:

DataFrame
2015 2016 2017 2018
QTR1 256 745 452 1021
QTR2 452 785 474 958
QTR3 635 478 725 528
QTR4 965 547 854 425
Sales of 2017
QTR1 452
QTR2 474
QTR3 725
QTR4 854
Name: 2017, dtype: int64
Sales in Quarter 2
2015 452
2016 785
2017 474
2018 958
Name: QTR2, dtype: int64
Sales in 2015, 2016, 2017 for Quarter 1 and 2
2015 2016 2017
QTR1 256 745 452
QTR2 452 785 474
Index of DataFrame
Index(['QTR1', 'QTR2', 'QTR3', 'QTR4'], dtype='object')
Columns of DataFrame
Index(['2015', '2016', '2017', '2018'], dtype='object')
Row and Column Labels
[Index(['QTR1', 'QTR2', 'QTR3', 'QTR4'], dtype='object'), Index(['2015', '2016', '2017',
'2018'], dtype='object')]
Number of Rows and Columns
(4, 4)
Sales of Quarter 4 During the Year 2018
425
Changing Sales in QTR3 During 2017 to 754
2015 2016 2017 2018
QTR1 256 745 452 1021
QTR2 452 785 474 958
QTR3 635 478 754 528
QTR4 965 547 854 425
Add a New Column 2019 With Values
2015 2016 2017 2018 2019
QTR1 256 745 452 1021 524
QTR2 452 785 474 958 639
QTR3 635 478 754 528 785
QTR4 965 547 854 425 458
PROGRAM 6:

AIM: Create a DataFrame with dictionary and replace all negative and NaN values. Their
values are [12,-55,67,78,nan,-12] and [56,-78,89,90,nan,87] respectively. Display the
DataFrame.

I) Replace all negative values with 0


II) Replace all nan values with 689

CODE:
OUTPUT:

PROGRAM 7:

AIM:Create a DataFrame quarterly sales where rows are itemcategory, itemname and expenditure and
index as [Qtr1, Qtr2, Qtr3, Qtr4] and perform these operations:

I) Display the column itemname

II) Display the 2nd row of the DataFrame quarterly sales

III) Add a new row ‘Qtr5’ with values being [Biscuits,Lotus,550]

IV) Delete a row ‘Qtr4’.


V) Rename the column ‘itemname’ to ‘Brandname’.

CODE:

OUTPUT:
ItemCategory ItemName Expenditure
Qtr1 Ice Cream Baskin Robbins 150
Qtr2 Cookies Nestle 300
Qtr3 Chocolate Dairy Milk 600
Qtr4 Juice Suntop 400
The column ItemName:
Qtr1 Baskin Robbins
Qtr2 Nestle
Qtr3 Dairy Milk
Qtr4 Suntop
Name: ItemName, dtype: object
2nd row of qurterly sales:
A new row Qtr5 with given information:
ItemCategory ItemName Expenditure
Qtr1 Ice Cream Baskin Robbins 150
Qtr2 Cookies Nestle 300
Qtr3 Chocolate Dairy Milk 600
Qtr4 Juice Suntop 400
Qtr5 Biscuits Lotus 550
Delete the row Qtr4:
ItemCategory ItemName Expenditure
Qtr1 Ice Cream Baskin Robbins 150
Qtr2 Cookies Nestle 300
Qtr3 Chocolate Dairy Milk 600
Qtr5 Biscuits Lotus 550
Renaming the column 'ItemName' to 'BrandName':
ItemCategory BrandName Expenditure
Qtr1 Ice Cream Baskin Robbins 150
Qtr2 Cookies Nestle 300
Qtr3 Chocolate Dairy Milk 600
Qtr4 Juice Suntop 400
Qtr5 Biscuits Lotus 550

Program 11:

AIM: Create two dataframes from dictionaries and combine them using concat method
in pandas.

CODE:
OUTPUT:
Name Age Address Qualification
1 Jai 27 Nagpur MSc
2 Princi 24 Kanpur MA
3 Gaurav 22 Allahabad MCA
4 Anuj 32 Kannauj PhD
Name Age Address Qualification
5 Abhi 28 Nagpur BTech
6 Ayushi 31 Kanpur BA
7 Dhiraj 26 Allahabad BCom
8 Hitesh 25 Kannauj BHons
Name Age Address Qualification
1 Jai 27 Nagpur MSc
2 Princi 24 Kanpur MA
3 Gaurav 22 Allahabad MCA
4 Anuj 32 Kannauj PhD
5 Abhi 28 Nagpur BTech
6 Ayushi 31 Kanpur BA
7 Dhiraj 26 Allahabad BCom
8 Hitesh 25 Kannauj BHons
In [ ]:

PROGRAM 13:

AIM: Program to select or filter rows froma Dataframe:

A) Based on values in columns using Relational and Logical Operators


B) Based on a criteria such as duplicate rows

Code:

A) Based on values in columns using Relational and Logical Operators

OUTPUT:

Original DataFrame
Name Percentage Qualify
0 Aman 79.5 Yes
1 Kamal 29.0 No
2 Amjad 90.5 Yes
3 Rohan NaN No
4 Amit 32.0 No
5 Sumit 65.0 Yes
6 Matthew 56.0 Yes
7 Kartik NaN No
8 Kavita 29.0 No
9 Pooja 89.0 Yes

Use==operator

Name Percentage Qualify


3 Rohan NaN No

Use < operator

Name Percentage Qualify


1 Kamal 29.0 No
4 Amit 32.0 No
8 Kavita 29.0 No

Use != operator

Name Percentage Qualify


0 Aman 79.5 Yes
2 Amjad 90.5 Yes
5 Sumit 65.0 Yes
6 Matthew 56.0 Yes
9 Pooja 89.0 Yes

Multiple Conditions

Name Percentage Qualify


1 Kamal 29.0 No
4 Amit 32.0 No
8 Kavita 29.0 No

b). Filter out rows based on different criteria such as duplicate rows

Q 21). Create an array having 30 elements between 5 and 50.Plot the following having 10 bins.

i).Simple histogram

ii).Horizontal histogram

iii).Step-Type Histogram

iv).Cumulative Histogram

CODE;
Q 20.) Plotting a Histogram.

The following seat bookings are the daily records of a month December from PVR cinemas;

124,124,135,156,128,189,200,150,158,150,200,124,143,142,130,130,170,189,200,130,142,167,180,143,
143,135,156,150,200,189,189,142

Construct a histogram from above data with 10 bin.

CODE;
Q.19) Create a bar chart to display data of Steve Smith,Kane Williamson& Jos Butler. Customize Chart as
per your wish.(Consider the dataframe of program 18)

You might also like