1. Practical File-Python
1. Practical File-Python
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:
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:
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:
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:
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:
6
Output:
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:
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:
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:
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:
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