Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

IP Practical PRGM

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

1. CREATE AN EMPTY SERIES USING PANDAS.

Source Code: -

import pandas as pd
s1 = pd.Series()
print(s1)

Output:

Sereies ([], dtype: float64)


2. CREATE A SERIES USING LIST.

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:

RESTART: C:/Users/preeti g_series1.py1


a 1
b 2
c 3
dtype: int 64
c 3
d 4
e 5
dtype: int 64
5. TO CREATE A SERIES FROM A SCALAR OR CONSTANT VALUE

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: -

import pandas as pd1


data1= [ [‘Shreya’, 20], [‘Rakshit’, 22], [‘Srijan’, 18]]
df1= pd1.DataFrame (data1, columns= [‘Name’, ‘Age’,])
print(df1)

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

Series generated as:


{‘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]}
10.TO CREATE A DATA FRAME BY PASSING A LIST OF
DICTIONARIES.

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

Aditya Ajay Pankaj Rinku Ritu


0 92.0 75 88 67 78.0
1 NaN 87 65 77 58.0
2 70.0 67 74 88 NaN
11. MATHEMATICAL OPERATIONS

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:

Addition Subtraction Multiplication Division Power

p 3.0 1.0 2.0 2.0 2.0


q 1.0 1.0 0.0 inf 1.0
r 2.0 2.0 0.0 inf 1.0
s 2.0 -2.0 0.0 0.0 0.0
t 1.0 1.0 0.0 0.0 0.0
12.CREATE A PANDAS SERIES FROM A DICTIONARY OF VALUES
AND AN NDARRAY

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:

RESTART: F:\ip practical\program1.py


Series Object is:
pen 10
Pencil 12
Notebook 15
dtype: int64
Series Object 2 is:
A 2
B 4
C 6
D 8
dtype: int32
13.GIVEN A SERIES, PRINT ALL THE ELEMENTS THAT ARE ABOVE
THE 75TH PERCENTILE.

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:

RESTART: F:\ip practical\program2.py


75th quantile value is: 81.75
Value that are greater than 75th quantile value are:
85 100

14. CREATE A DATAFRAME QUARTERLY SALES WHERE EACH ROW


CONTAINS THE ITEM CATEGORY, ITEM NAME, AND
EXPENDITURE.GROUP THE ROWS BY THE CATEGORY AND PRINT
THE TOTAL EXPENDITURE PER CATEGORY.

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:

RESTART: F:\ip practical\program3.py


Item Category Item Name Expenditure
0 Food Biscuit 100
1 Drink Pepsi 30
2 Food Bread 80
3 Drink Cocacola 200
4 Sweet Rasgulla 80
5 Food Butter 100
6 Sweet Butter Milk 300
Item Category
Drink 230
Food 280
Sweet 380
Name: Expenditure, dtype: int64
Expenditure
Item Category
Drink 230
Food 280
Sweet 380
15.IMPORTING AND EXPORTING DATA BETWEEN PANDAS AND CSV
FILE.

Source Code: -

# Importing csv file

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")

# Exporting csv file

import pandas as pd

df =pd.read_csv("f:\\program7.csv")

print(df)
Output:

Importing a CSV file

Exporting a CSV file

Unnamed: 0 RollNo SName Marks

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: -

import matplotlib.pyplot as plt

x = [1,2,3]

y= [5,7,4]

Pl. plot (x,y,label= 'First line')

x2= [1,2,3]

y2=[10,11,14]

plt.plot(x2,y2,label= 'Second line')

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 matplotlib.pyplot as plt

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.xlabel('Item (s) ')


plt.ylabel('Value')

plt.title('\n\n Second Chart')

plt.grid(True)

plt.show()

Output: -
18.TO PLOT A PIE CHART FOR THE POPULAR LANGUAGES AMONG
THE STUDENTS

Source Code: -

import matplotlib.pyplot as plt

labels='Python', 'C++', 'Ruby', 'Java'

sizes= [215,130,245,210]

colors= ['gold','yellowgreen', 'lightcoral', 'lightskyblue']

explode= (0.1,0,0,0)

plt.pie(sizes, explode=explode, labels=labels, colors=colors, shadow=True,


startangle=140)

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 matplotlib.pyplot as plt

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.title(' Performance of students')

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

import matplotlib.pyplot as plt

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.title("Student's mark sheet")

plt.show()
Output: -
DATA MANAGEMENT (My SQL)

TO CREATE A DATABASE: -

mysql> create database student;

Query OK, 1 row affected (0.01 sec)

TO OPENING A DATABASE: -

mysql> use student;

Database changed

TO CREATING A TABLE:-

mysql> create table stud(sid int(10) unsigned auto_increment primary key not null,

sname char(10),smarks int(5));

Query OK, 0 rows affected (0.18 sec)

TO VIEWING A TABLE STRUCTURE:-

mysql> Desc stud;

+--------+------------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+--------+------------------+------+-----+---------+----------------+

| sid | int(10) unsigned | NO | PRI | NULL | auto_increment |

| sname | char(10) | YES | | NULL | |

| smarks | int(5) | YES | | NULL | |

+--------+------------------+------+-----+---------+----------------+

3 rows in set (0.15 sec)


TO INSERTING DATA INTO A TABLE:-

mysql> insert into stud values (101, 'John', 87);

Query OK, 1 row affected (0.05 sec)

mysql> insert into stud values(102,'Akbar',72);

Query OK, 1 row affected (0.00 sec)

mysql> insert into stud values(103,'Vinak', 65);

Query OK, 1 row affected (0.00 sec)

mysql> insert into stud values(104,'Ashwin',56);

Query OK, 1 row affected (0.00 sec)

mysql> insert into stud values(106,'Sharmi',96);

Query OK, 1 row affected (0.00 sec)

mysql> insert into stud values(107, 'Kalees',52);

Query OK, 1 row affected (0.00 sec)

mysql> insert into stud values(108,'Kayathiri',63);

Query OK, 1 row affected (0.00 sec)


mysql> insert into stud values(109,'Ram',73);

Query OK, 1 row affected (0.00 sec)

mysql> insert into stud values(110,'Raji',99);

Query OK, 1 row affected (0.00 sec)

TO DISPLAY THE TABLE :-

mysql> select * from stud ;

+-----+-----------+--------+

| sid | sname | smarks |

+-----+-----------+--------+

| 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 |

+-----+-----------+--------+

9 rows in set (0.00 sec)


TO DELETE THE DETAILS:-

mysql> delete from stud where sid=103;

Query OK, 1 row affected (0.09 sec)

mysql> select * from stud;

+-----+-----------+--------+

| sid | sname | smarks |

+-----+-----------+--------+

| 101 | John | 87 |

| 102 | Akbar | 72 |

| 104 | Ashwin | 56 |

| 106 | Sharmi | 96 |

| 107 | Kalees | 52 |

| 108 | Kayathiri | 63 |

| 109 | Ram | 73 |

| 110 | Raji | 99 |

+-----+-----------+--------+

8 rows in set (0.00 sec)


USE THE SELECT COMMAND TO GET THE DETAILS OF THE
STUDENTS WITH MARKS MORE THAN 80.

mysql> select * from stud where smarks>80;

+-----+--------+--------+

| sid | sname | smarks |

+-----+--------+--------+

| 101 | John | 87 |

| 106 | Sharmi | 96 |

| 110 | Raji | 99 |

+-----+--------+--------+

3 rows in set (0.00 sec)

TO FIND MINIMUM VALUE:-

mysql> select min(smarks) from stud;

+-------------+

| min(smarks) |

+-------------+

| 52 |

+-------------+

1 row in set (0.07 sec)


TO FIND MAXIMUM VALUE:-

mysql> select max(smarks) from stud;

+-------------+

| max(smarks) |

+-------------+

| 99 |

+-------------+

1 row in set (0.00 sec)

TO FIND SUM VALUE:-

mysql> select sum(smarks) from stud;

+-------------+

| sum(smarks) |

+-------------+

| 598 |

+-------------+

1 row in set (0.11 sec)


TO FIND AVERAGE VALUE:-

mysql> select avg(smarks) from stud;

+-------------+

| avg(smarks) |

+-------------+

| 74.7500 |

+-------------+

1 row in set (0.00 sec)

WRITE A SQL QUERY TO ORDER THE (STUDENT ID, MARKS) TABLE


IN DESCENDING ORDER OF THE MARKS

mysql> select sid,sname,smarks from stud order by sid,smarks desc;

+-----+-----------+--------+

| sid | sname | smarks |

+-----+-----------+--------+

| 101 | John | 87 |

| 102 | Akbar | 72 |

| 104 | Ashwin | 56 |

| 106 | Sharmi | 96 |

| 107 | Kalees | 52 |

| 108 | Kayathiri | 63 |

| 109 | Ram | 73 |

| 110 | Raji | 99 |
+-----+-----------+--------+

8 rows in set (0.11 sec)

mysql> create database customers;

Query OK, 1 row affected (0.11 sec)

mysql> use customers;

DATABASE CHANGED

mysql> create table cust(cid int(10) unsigned auto_increment primary key not null,
cname varchar(20), country varchar(20));

Query OK, 0 rows affected (0.05 sec)

mysql> desc cust;

+---------+------------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+---------+------------------+------+-----+---------+----------------+

| cid | int (10) unsigned | NO | PRI | NULL | auto_increment |

| cname | varchar (20) | YES | | NULL | |

| country | varchar (20) | YES | | NULL | |

+---------+------------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)


mysql> insert into cust values(1,'Parthiban', 'India');

Query OK, 1 row affected (0.00 sec)

mysql> insert into cust values(2,'Naveen','USA');

Query OK, 1 row affected (0.00 sec)

mysql> insert into cust values(3,'Iniya','USA');

Query OK, 1 row affected (0.00 sec)

mysql> insert into cust values(4,'Dharsan','Canada');

Query OK, 1 row affected (0.00 sec)

mysql> insert into cust values(5,'Ammu','India');

Query OK, 1 row affected (0.00 sec)

mysql> insert into cust values(6,'Priya','USA');

Query OK, 1 row affected (0.00 sec)

mysql> insert into cust values(7,'Akshya','India');

Query OK, 1 row affected (0.00 sec)


mysql> insert into cust values(8,'Santhosh', 'India');

Query OK, 1 row affected (0.00 sec)

mysql> select * from cust ;

+-----+-----------+---------+

| cid | cname | country |

+-----+-----------+---------+

| 1 | Parthiban | India |

| 2 | Naveen | USA |

| 3 | Iniya | USA |

| 4 | Dharsan | Canada |

| 5 | Ammu | India |

| 6 | Priya | USA |

| 7 | Akshya | India |

| 8 | Santhosh | India |

+-----+-----------+---------+

8 rows in set (0.00 sec)


Find the total number of customers from each country in the table

mysql> select country, count(*) from cust group by country;

+---------+----------+

| country | count(*) |

+---------+----------+

| Canada | 1|

| India | 4|

| USA | 3|

+---------+----------+

3 rows in set (0.00 sec)

You might also like