IP Practical PRGM
IP Practical PRGM
IP Practical PRGM
Source Code: -
import pandas as pd
s1 = pd.Series()
print(s1)
Output:
Source Code: -
import pandas as pd
series1 = pd.Series ([10,20,30,40])
print (series1)
Output:
0 10
1 20
2 30
3 40
dtype: int64
3. CREATE A SERIES USING RANGE ( ) METHOD.
Source Code: -
import pandas as pd
series1 = pd.Series (range(5))
print(series1)
Output:
0 0
1 1
2 2
3 3
4 4
dtype: int64
4. PERFORM INDEXING, SLICING AND ACCESSING DATA FROM A
SERIES
Source Code: -
import pandas as pd
s=pd.Series ([1,2,3,4,5], index=[‘a’,’b’,’c’,’d’,’e’])
print (s [0])
print (s [:3])
print (s [-3:])
Output:
Source Code: -
import pandas as pd
series1 =pd.Series(55, index=[‘a’,’b’,’c’,’d’,’e’])
print(series1)
Another Format
series2=pd.Series(55, index=[0,1,2,3])
Print(series2)
Output:
RESTART: C:/Users/preeti.py
a 55
b 55
c 55
d 55
e 55
dtype: int64
0 55
1 55
2 55
3 55
dtype: int 64
6. TO CREATE A SERIES FROM TWO DIFFERENT LISTS.
Source Code: -
import pandas as pd
months = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’]
no_days = [31,28,31,30,31,30]
series = pd.Series(no_days, index = months)
print(series)
Output:
RESTART: C:/Users/preeti/2lists.py
Jan 31
Feb 28
Mar 31
Apr 30
May 31
Jun 30
dtype: int64
7. TO CREATE AN EMPTY DATA FRAME
Source Code: -
import pandas as pd
df1 =pd.DataFrame()
print(df1)
Output:
RESTART: C:/Users/preeti,
Empty DataFrame
Columns: []
Index: []
8.CREATING DATA FRAME FROM LISTS
Source Code: -
Output:
RESTART: C:/Users/preeti.py
Name Age
0 Shreya 20
1 Rakshit 22
2 Srijan 18
9.CREATING DATA FRAME FROM DICTIONARY (DICTIONARY OF
SERIES)
Source Code: -
import pandas as pd
student = {‘Name’: [‘Rinku’, ‘Rintu’, ‘Ajay’, ‘Pankaj’, ‘Aditya’],
‘English’: [67,78,75,88,92], ‘Economics’: [78,67,89,90,56], ‘IP’:
[78,88,98,90,87], ‘Accounts’:[77,70,80,67,86]}
print (“Series generated as: “)
print(student)
Output:
RESTART:
C:/Users/preeti/AppData/Local/Programs/Python/Python37-32/prog_dic
_df.Py
Source Code: -
import pandas as pd
new student = [{‘Rinku’ :67, ‘Rintu’ :78, ‘Ajay’ :75, ‘Pankaj’ :88,
‘Aditya’ :92},
{‘Rinku’ : 77, ‘Rintu’ :58, ‘Ajay’ :87, ‘Pankaj’ :65},
{‘Rinku’, :88, ‘Rintu’ : 88, ‘Ajay’ :67, ‘Pankaj’ 74,
‘Aditya’ : 70}]
newdf = pd.DataFrame(new student)
print(newdf)
Output:
RESTART: C:/Users/preeti/AppData/Local, List.py
Source Code: -
import numpy as np
import pandas as pd
x= pd.Series([2,1,2,np.nan], index=[‘p’,’q’,’r’,’s’])
y= pd.Series([1,np.nan,2,1], index=[‘p’,’q’,’s’,’t’])
print(“----Addition----“)
print(x.add(y,fill_value=0))
print(“----Subtraction----“)
print(x.sub(y,fill_value=0))
print(“----Multiplication----“)
print(x.mul(y,fill_value=0))
print(“----Divisio----“)
print(x.div(y,fill_value=0))
print(“----Power----“)
print(x.pow(y,fill_value=0))
Output:
Source Code: -
import pandas as pd
import numpy as np
item={"pen":10,"Pencil":12,"Notebook":15}
ser1= pd.Series(item)
print("Series Object is :")
print(ser1)
arr=np.array([2,4,6,8])
ser2= pd.Series(arr,index=['A','B','C','D'])
print("Series Object 2 is : ")
print(ser2)
Output:
Source Code: -
import pandas as pd
import numpy as np
arr=np.array([42,12,72,85,56,100])
ser1= pd.Series(arr)
quantile_value=ser1.quantile(q=0.75)
print("75th quantile value is : ",quantile_value)
print("Value that are grater than 75th quantile value are : ")
for val in ser1:
if(val>quantile_value):
print(val,end=" ")
Output:
Source Code: -
import pandas as pd
sales_data={
"Item Category":["Food", "Drink", "Food", "Drink", "Sweet",
"Food", "Sweet"],
"Item Name":["Biscuit", "Pepsi", "Bread", "Cocacola",
"Rasgulla", "Butter", "Butter Milk"],
"Expenditure":[100,30,80,200,80,100,300]
}
sales_quaterly = pd.DataFrame(sales_data)
print(sales_quaterly)
gdf= sales_quaterly.groupby("Item Category")
resulted_df = gdf['Expenditure'].sum()
print(resulted_df)
pivoted_df = sales_quaterly.pivot_table(index="Item
Category",values="Expenditure",aggfunc="sum")
print(pivoted_df)
Output:
Source Code: -
import pandas as pd
data1={
'Roll No':[1,2,3,4,5],
'SName':['AAA','BBB','CCC','DDD','EEE'],
'Marks':[65,45,95,85,75]
df =pd.DataFrame(data1,columns=['Roll No','SName','Marks'])
df.to_csv("f:\\stud1.csv")
import pandas as pd
df =pd.read_csv("f:\\program7.csv")
print(df)
Output:
0 0 1 AAA 65
1 1 2 BBB 45
2 2 3 CCC 95
3 3 4 DDD 85
4 4 5 EEE 75
16. TO ADD LEGENDS, TITLES AND LABELS TO A LINE PLOT WITH
MULTIPLE LINES
Source Code: -
x = [1,2,3]
y= [5,7,4]
x2= [1,2,3]
y2=[10,11,14]
plt.xlabel('Plot number')
plt.ylabel('Important')
plt.title('New Graph')
plt.legend()
plt.show()
Output: -
17. TO PLOT TWO LINES IN TWO DIFFERENT VIEWS OF THE SAME
WINDOW
Source Code: -
import numpy as np
t= np.arange(0.0,20.0,1)
s= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
s2= [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
plt.subplot(2,1,1)
plt.plot(t, s)
plt.ylabel('Value')
plt.title('First Chart')
plt.grid(True)
plt.subplot(2,1,2)
plt.plot(t, s2)
plt.grid(True)
plt.show()
Output: -
18.TO PLOT A PIE CHART FOR THE POPULAR LANGUAGES AMONG
THE STUDENTS
Source Code: -
sizes= [215,130,245,210]
explode= (0.1,0,0,0)
plt.axis('equal')
plt.show()
Output: -
19. GIVEN THE SCHOOL RESULT DATA, ANALYSES THE
PERFORMANCE OF THE STUDENT ON DIFFERENT PARAMETERS,
EXAMPLE SUBJECT VOICE OR CLASS VOICE
Source Code: -
import numpy as np
objects = ('Tamil','English','Commerce','Accounts','Computer')
y_pos=np.arange(len(objects))
Types = (65,80,75,75,62)
plt.bar(y_pos,Types,align='center',color='yellow')
plt.xticks(y_pos,objects)
plt.ylabel('Marks')
plt.xlabel('Subjects')
plt.show()
Output: -
20. FOR THE DATAFRAMES CREATE A ABOVE, ANALYZE, AND
PLOT APPROPRIATE CHARTS WITH TITLE AND LEGEND
Source Code: -
import pandas as pd
data={
"Name":["Ankit","Parul","Abhishek","Rohan"],
"Subject1":[78,89,77,82],
"Subject2":[67,90,72,80]
df =pd.DataFrame(data)
plt.ylim(60,100)
df.plot(kind='bar')
plt.xlabel("Names")
plt.ylabel("Marks")
plt.show()
Output: -
DATA MANAGEMENT (My SQL)
TO CREATE A DATABASE: -
TO OPENING A DATABASE: -
Database changed
TO CREATING A TABLE:-
mysql> create table stud(sid int(10) unsigned auto_increment primary key not null,
+--------+------------------+------+-----+---------+----------------+
+--------+------------------+------+-----+---------+----------------+
+--------+------------------+------+-----+---------+----------------+
+-----+-----------+--------+
+-----+-----------+--------+
| 101 | John | 87 |
| 102 | Akbar | 72 |
| 103 | Vinak | 65 |
| 104 | Ashwin | 56 |
| 106 | Sharmi | 96 |
| 107 | Kalees | 52 |
| 108 | Kayathiri | 63 |
| 109 | Ram | 73 |
| 110 | Raji | 99 |
+-----+-----------+--------+
+-----+-----------+--------+
+-----+-----------+--------+
| 101 | John | 87 |
| 102 | Akbar | 72 |
| 104 | Ashwin | 56 |
| 106 | Sharmi | 96 |
| 107 | Kalees | 52 |
| 108 | Kayathiri | 63 |
| 109 | Ram | 73 |
| 110 | Raji | 99 |
+-----+-----------+--------+
+-----+--------+--------+
+-----+--------+--------+
| 101 | John | 87 |
| 106 | Sharmi | 96 |
| 110 | Raji | 99 |
+-----+--------+--------+
+-------------+
| min(smarks) |
+-------------+
| 52 |
+-------------+
+-------------+
| max(smarks) |
+-------------+
| 99 |
+-------------+
+-------------+
| sum(smarks) |
+-------------+
| 598 |
+-------------+
+-------------+
| avg(smarks) |
+-------------+
| 74.7500 |
+-------------+
+-----+-----------+--------+
+-----+-----------+--------+
| 101 | John | 87 |
| 102 | Akbar | 72 |
| 104 | Ashwin | 56 |
| 106 | Sharmi | 96 |
| 107 | Kalees | 52 |
| 108 | Kayathiri | 63 |
| 109 | Ram | 73 |
| 110 | Raji | 99 |
+-----+-----------+--------+
DATABASE CHANGED
mysql> create table cust(cid int(10) unsigned auto_increment primary key not null,
cname varchar(20), country varchar(20));
+---------+------------------+------+-----+---------+----------------+
+---------+------------------+------+-----+---------+----------------+
+---------+------------------+------+-----+---------+----------------+
+-----+-----------+---------+
+-----+-----------+---------+
| 1 | Parthiban | India |
| 2 | Naveen | USA |
| 3 | Iniya | USA |
| 4 | Dharsan | Canada |
| 5 | Ammu | India |
| 6 | Priya | USA |
| 7 | Akshya | India |
| 8 | Santhosh | India |
+-----+-----------+---------+
+---------+----------+
| country | count(*) |
+---------+----------+
| Canada | 1|
| India | 4|
| USA | 3|
+---------+----------+