0% found this document useful (0 votes)
5 views14 pages

1. Practical File-Python

The document outlines various practical exercises related to data manipulation using the Pandas library in Python. It includes tasks such as creating dataframes from series and dictionaries, performing mathematical operations on series, filtering data, and visualizing data with charts and histograms. Each section provides an aim, algorithm, pseudocode, and expected output for clarity.

Uploaded by

valli2809.2008
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)
5 views14 pages

1. Practical File-Python

The document outlines various practical exercises related to data manipulation using the Pandas library in Python. It includes tasks such as creating dataframes from series and dictionaries, performing mathematical operations on series, filtering data, and visualizing data with charts and histograms. Each section provides an aim, algorithm, pseudocode, and expected output for clarity.

Uploaded by

valli2809.2008
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/ 14

IP PRACTICAL FILE- TERM I

1. Heading: Creating a Dataframe from Series


Aim:
To Create a dataframe from pandas series and add series externally.
Algorithm:
Step 1: start
Step 2: import pandas library
Step 3: create lists of author and article
Step3: create the series for author and article and assign to other variables
Step 4: create a dictionary for the series
Step5: create a dataframe for the series
Step 6:create a list age
Step 7: Create a series for the list age to be added to dataframe.
Step 8:Display the result
Step 9: stop
Pseudocode:
import pandas as pd
author = ['Jitender', 'Purnima', 'Arpit', 'Jyoti']
article = [210, 211, 114, 178]
au_s = pd.Series(author)
ar_s = pd.Series(article)
frame = { 'Author': au_s, 'Article': ar_s }
result = pd.DataFrame(frame)
age = [21, 21, 24, 23]
result['Age'] = pd.Series(age)
print(result)

Output:
Author Article Age
0 Jitender 210 21
1 Purnima 211 21
2 Arpit 114 24
3 Jyoti 178 23

1
2. Heading: Conversion of a Dictionary to Series
Aim:
To write a Pandas program for conversion of a dictionary to a Pandas series.
Sample dictionary: d1 = {'a': 100, 'b': 200, 'c':300}
Algorithm:
Step 1: Start
Step 2: Import pandas library
Step 3: Create a dictionary
Step 4: Convert the dictionary into series
new_series = pd.Series(d1)
step 5: Display the series
Step 6: stop
Pseudocode:
import pandas as pd
d1 = {'a': 100, 'b': 200, 'c':300}
print("Original dictionary:")
print(d1)
new_series = pd.Series(d1)
print("Converted series:")
print(new_series)

output:

3. Heading: Mathematical operations on Series


Aim: To perform mathematical operations by taking two series .
Algorithm:
Step 1: Start
Step 2: Import pandas library
Step 3: Create two series ds1 and ds2
Step 4: Performing mathematical calculations like Addition, Subtraction,
Multiplication and division
Step 5: Displaying ds
Step 6: Stop

2
Pseudocode:
import pandas as pd
ds1=pd.Series([2,4,6,8,10])
ds2=pd.Series([1,3,5,7,9])
ds = ds1 + ds2
print("Add two Series:") #adding two series
print(ds)
print("Subtract two Series:") #subtracting two series
ds=ds1-ds2
print(ds)
print("Multiplying two series") #multiplying two series
ds=ds1*ds2
print(ds)
print("Dividing two series") #dividing two series
ds=ds1/ds2
print(ds)
Output:

4. Heading: Dataframe-Two dimensional list


Aim:
To make a pandas dataframe with two-dimensional list.

3
Algorithm:
Step 1:start
Step 2: Import pandas library
Step 3: create a nested list
lst = [['apple', 25], ['mango', 30],
['strawberry', 26], ['pineapple', 22]]

Step 4: Give the column names as ‘Tag’ and ‘number’ while converting to dataframe
Step 5: Display dataframe
Step 6: stop
Pseudocode:
import pandas as pd
lst = [['apple', 25], ['mango', 30],
['strawberry', 26], ['pineapple', 22]]
df = pd.DataFrame(lst, columns =['Tag', 'number'])

print(df )
output:

5. Heading: Dataframe from a dictionary


Aim:
To Create dataframe from dictionary of ndarray/list.
Algorithm:
Step 1: Start
Step 2: import pandas library
Step 3: Create a dictionary by using a list
Step 4: Convert the dictionary into dataframe
Step 5: Display the dataframe
Step 6: Stop
Pseudocode:
import pandas as pd
data = {'Area':['Array', 'Stack', 'Queue'],
'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]}
df = pd.DataFrame(data, index =['Cat_1', 'Cat_2', 'Cat_3'])
print(df)
Output:

4
Area Student_1 Student_2
Cat_1 Array 20 15
Cat_2 Stack 21 20
Cat_3 Queue 19 14
6. Heading: Filtering the rows based on different criteria.
Aim:
To create the following data frame and to display the details of Koti.

Algorithm:
Step 1 : Start
Step 2: Import Pandas library
Step 3: Create the dictionary STU with the data given.
Step 4: Convert the dictionary into dataframe by adding the index.
Step 5: Create another dataframe with Koti by using the previous dataframe.
Step 6: Display the new dataframe.
Step 7: Stop.
Pseudocode:
import pandas as pd
STU={'Sno':[10,20,30,40],'Sname':['Harshith','koti','John','Charles'],'Grade':['A','C','B','C'
]}
df=pd.DataFrame(STU,index=['grape','apple','mango','orange'])
print(df)
df1=df[df['Sname']=='koti']
print(df1)
Output:

7. Heading: Adding a column in a Dataframe


Aim:
To create the dataframe with the following data

5
To the above dataframe add a column density with the values
['1500','1219','1630','1050']
import pandas as pd
df=[['10927986','72167810927986'],['12691836','85087812691836'],['4631392','42267
84631392'],['4328063','5261784328063']]
df1=pd.DataFrame(df,index=['Delhi','Mumbai','Kolkata','Chennai'],columns=['Populatio
n','Avg_Income'])
print(df1)
df1['Density']=['1500','1219','1630','1050']
print(df1)
Output:

8. Heading: Head and Tail functions on Dataframe


Aim: To perform head and tail functions on Dataframe
Algorithm:
Step 1: Start
Step 2: Import pandas library
Step 3: Creating a dictionary with student details.
Step 4: Display the whole dataframe.
Step 5: Display first 3 rows by using head function
Step 6: Display last 4 rows by using tail function
Pseudocode:
import pandas as pd
data={'Sno':[11,12,13,14,15],'Sname':['Lakshya','Supreet','Tanvi','Sakshi','Nirali'],'Marks
':[80,67,58,96,99],'Group':['Commerce','Science','Commerce','Art','Science']}
df=pd.DataFrame(data)
print(df)
print(df.head(3))
print(df.tail(4))

6
Output:

9. Heading:Data transfer from CSV to Dataframe


Aim:
To transfer the following data from CSV to Data Frame:
Organization CEO Established
0 Alphabet Sundar Pichai 02-Oct-15
1 Microsoft Satya Nadella 04-Apr-75
2 Amazon Jeff Bezos 05-Jul-94

Algorithm:
Step 1:Start
Step 2:Import pandas library
Step 3:Give the file path where the CSV is located in read_csv() and assign to data
variable.
Step 4:Represent column headings
Step 5:Display data
Step 6:Stop
Pseudocode:
import pandas as pd
data = pd.read_csv('E:\KEN_1\XII\IP practicals New 2020\Practical_5.csv')
data.columns=['Organization','CEO','Established']
print(data)
Output:

7
10. Heading:Data transfer from Dataframe to CSV file.
Aim:
To Store the data into a CSV file with seperator as ‘$’:
Algorithm:
Step 1:Start
Step 2:Import pandas library
Step 3:Create a dictionary with student data
Step 4:Convert that dictionary into dataframe
Step 5:Mention the path where the file has to be saved in to_csv()
Step 6:Display Dataframe.
Step 7: Stop
Pseudocode:
import pandas as pd
dic={'Roll_no':[101,102,103,104],'Name':['Ani','Amna','Arjan','Mark'],
'Surname':['Kapur','Sharif','Singh','Robin']}
df=pd.DataFrame(dic)
df.to_csv('E:\KEN_1\XII\IP practicals New 2020\Practical_6.csv',sep="$")
print(df)
Output:

11. Heading: Creating a Line chart


Aim:
To Create a Line chart based on the following student data :

Roll no. Marks

21 99

22 87

8
23 96

24 90

25 88

26 70

27 91

Algorithm:
Step 1:Start
Step 2:Import matplotlib,pyplot library
Step 3:Create lists for rollno,marks
Step 4:use the function plot(rollno,Marks) for line chart
Step 5:Assign title,xlabel,ylabel to the chart
Step 6:Display the chart
Step 7: stop
Pseudocode:
import matplotlib.pyplot as pl
rollno=[21,22,23,24,25,26,27]
Marks=[99,87,96,90,88,70,91]
pl.plot(rollno,Marks)
pl.title("Student Details")
pl.xlabel("Roll number")
pl.ylabel("Marks")
pl.show( )

Output:

9
12. Heading: Creating a Bar chart
Aim:
To create a bar diagram based on the given employee data:
empno sal

1287 80000

1288 100000

1289 90000

1290 75000

1291 150000

1292 200000

1293 100000

Algorithm:
Step 1:Start
Step 2: Import matplotlib, pyplot library
Step 3: Create lists of empno, sal
Step 4: Create barchart by using bar(empno,sal)
Step 5: Assign title, xlabel, ylabel to the chart
Step 6: Display the chart
Step 7: Stop
Pseudocode:
import matplotlib.pyplot as pl

10
empno=[1287,1288,1289,1290,1291,1292,1293]
sal=[80000,100000,90000,75000,150000,200000,100000]
pl.bar(empno,sal)
pl.title("Employee Details")
pl.xlabel("Employee no.")
pl.ylabel("Salary")
pl.show( )

Output:

13. Heading: Creating a histogram


Aim:
To create a histogram for the given data:
Weight measurements for 16 small orders of french fries (in grams):
78 72 69 81 63 67 65 75
79 74 71 83 71 79 80 69

Algorithm:
Step 1:Start
Step 2: Import matplotlib,pyplot library
Step 3: Create a list with 16 sample weights
Step 4: create a histogram by using hist(Weight)
Step 5: Assign title for the chart.
Step 6:Display the chart

11
Step 7: Stop
Pseudocode:
import matplotlib.pyplot as pl
Weight=[78,72,69,81,63,67,65,75,79,74,71,83,71,79,80,69]
pl.hist(Weight)
pl.title("Weight Measurements")
pl.show( )

Output:

14. Heading: Creating a bar chart for school data


Aim:
To write a program to plot a bar chart in python to display the result of a school for
five consecutive years.
Algorithm:
Step 1: Start
Step 2: Import matplotlib, pyplot library
Step 3: Create lists of year,p(Pass percentage)
Step 4: Create barchart by using bar(year,p)
Step 5: Assign xlabel, ylabel to the chart
Step 6: Display the chart
Step 7: Stop
Pseudocode:
. import matplotlib.pyplot as pl
year=['2015','2016','2017','2018','2019']

12
p=[98.50,70.25,55.20,90.5,61.50]
pl.bar(year, p)
pl.xlabel("year")
pl.ylabel("Pass%")
pl.show( )

Output:

15. Heading : Creating Histogram for student Details.


Aim: To create a histogram for student details which are given below.
Name Min Marks Max Marks
Srikar 40 78
Sheela 42 72
Azhar 35 75
Bincy 26 80
Yash 32 69
Nazar 49 80
Algorithm:
Step 1: Start
Step 2: Import pandas, numpy, matplotlib libraries.
Step 3: Create a dictionary with the given data.
Step 4: Convert that dictionary into dataframe.
Step 5:Store Min marks in a variable minarray
Step 6: Create a histogram with y and edges.
Step 7: Calculate mid for edges
Step 8:Plot a histogram with title and x label.
Step 9: Stop

13
Pseudocode:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = {'Name':['Srikar', 'Sheela', 'Azhar', 'Bincy', 'Yash','Nazar'],'MIN marks' :
[40,42,35,26,32,49],'Max marks' : [78,72,75,80,69,80]}
df=pd.DataFrame(data)
minarray=np.array([df['MIN marks']])
y,edges = np.histogram(minarray)
mid = 0.5*(edges[1:]+ edges[:-1])
df.plot(kind='hist',y='MIN marks')
plt.plot(mid,y,'-^')
plt.title('Annual Min Marks')
plt.xlabel('Name')
plt.show()
Output:

14

You might also like