You are on page 1of 14

PYTHON PANDAS

(Practicals)

Example1: Write a program to create a series object that stores the initial
budget allocated (50000/each for the four quarters of the year: Qtr1 , Qtr2, Qtr3
and Qtr4.

Example2: Sequences section and comtri1 store the section


names(‘A’,’B’,’C’,’D’,’E’) and contribution made by them respectively
(6700,5600,5000,5200, nil) for a charity. Your school has decided to donate as
much contribution as made by each section ,i.e., the donation will be doubled.
Write code to create a Series object that stores the contribution amount as the
values and the section names as the indexes with datatype as float32.
Example3: Consider the two series objects s11 and s12 that you created in
examples 11and 12 respectively. Print the attributes of both these objects in a
report form as shown below:
Attribute name Object s11 Object s12
Data type
Shape
No. of bytes
No. of dimensions
Item size
Has NaNs ?

Example4: Consider a Series object s8 that stores the number of students in


each section of class 12.
A 39
B 41
C 42
D 44
Find two sections have been given a task of selling tickets @100/- per ticket as
part of a social experiment . Write code to display how much they have
collected.

Example 5: Consider the Series object s13 that stores the comtribution of each
section
6700 A
5600 B
5000 C

5200 D

Write code to modify the amount of section ‘A’ as 7600 and for sections ’C’
and ’D’ as 7000. Print the changed object.

Example 6: Number of students in classes 11 and 12 in three streams(‘Science’,


’Commerce’ and ‘Humanities’) are stored in two series objects c11 and
12.Write code to find total number of students in classes 11 and 12 ,stream
wise.
Example 7: Object1 Population stores the details of population in four metro
cities of India and Object2 AvgIncome stores the total average income
reported in previous year in each of these metros. Calculate income per capita
for each of these metro cities.

Example8:Series object s11 stores the charity contribution made by each


section:
A 6700

B 5600

C 5000
D 5200
Write a program to display which sections made a contribution more than
rupees 5500/-

Example 9: Create and display a Data Frame from a 2D dictionary, Sales, which
stores the quarterly sales as inner dictionary for two years, as shown below:
Sales={‘yr1’ : {‘Qtr1’: 34500,’Qtr2’: 5600,’Qtr3’: 47000,’Qtr4’: 49000},
‘yr2’ : {‘Qtr1’: 44900, ‘Qtr2’: 46100,’Qtr3’: 57000,’Qtr4 ’: 59000}

Example 10: Write a program to crate a dataframe from a list containing


dictionaries of the sales performance of four zonal offices. Zone names should
be the row labels.
Example 11: Write a program to create a Data Frame from a list containing
2lists, each containing Target and actual Sales figures of four zonal offices. Give
appropriate row labels.

Example 12: Write a program to create a Data Frame from a 2D array as shown
below:

101 113 124


130 140 200
115 216 217
Example 13: Write a program to create a DataFrame to store weight , age and
names of 3 people Print the DataFrame and its transpose.

Example 14 : Consider a dataframe df as shown .


Write statements to do the following :
Display rows 2 to 4 (both inclusive)
From rows 2 to 4 (both inclusive), display columns, ’Item Type’ and ‘Total
Profit’. From rows 2 to 5 `(both inclusive), display first four columns.
Display ‘Sales Channel’ and ‘Order ID’ of rows 1 and 5 only.
(i)>>> df [2:5]
(ii)>>> df . loc [2:4 , [‘Item Type’, ‘Total Profit”]]

(iii)>>> df . i loc[2:5,0:4]

(iv) >>> df loc[[1,5], [‘Sales Channel’ , ‘OrderId’]]


Example15: Given a Series object s5. Write a program to store the squares of
the Series values in object s6. Display s6’s values which are >15.

Example 16 : Consider the sale Df shown below.


Target Sales
zoneA 56000 58000
zoneB 70000 68000
zoneC 75000 78000
zoneD 60000 61000
Write a program to rename indexes of ‘zoneC’ and ‘zoneD’ as ‘Central’ and
‘Dakshin’ respectively and the column names ‘Target’ and ‘Sales’ as ‘Targeted’
and ‘Achieved’ respectively.

Example 17: Will the previous program reflect the renamed indexes and
columns’ names in the dataframe sale Df ? Make changes in the previous
program so that dataframe sale Df has the changed indexes and columns.

Example 18 : Given a series that stores the area of some states in km2. Write
code to find out the biggest and smallest three areas from the given Series.
Given series has been created like this :
Ser1= pd.Series ( [34567,890,450,67892,34677,78902,256711,678291,
637632,25723,2367,11789,345,256517])
Example 19 : Given a Series object s5.Write a program to calculate the cubes of
the Series values.

Example 19 : Given a Series Object s5. Write a program to store the squares of
the Series values in object s6. Display s6’s values which are > 15.
Example 20 : Write a program to display no. of rows and columns in the
DataFrame df.

Example 21 : Write a program to display the no. of rows and columns in


DataFrame df without using shape attribute.
Example 22: Given a Data Frame df :
Age Name Weight
0 15 Arnav 42
1 22 Charles 75
2 35 Guru 66

You might also like