0% found this document useful (0 votes)
127 views

Python Class-12th Practical Work (Connectivity and Dataframes)

this is the practical work of python of class 12th with connnectivity of MySQL and python questions in it. and dataframe funtion various questions. all are done by me without copying it from anywhere. so, yes, it's unique. THANKS!

Uploaded by

Priya Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Python Class-12th Practical Work (Connectivity and Dataframes)

this is the practical work of python of class 12th with connnectivity of MySQL and python questions in it. and dataframe funtion various questions. all are done by me without copying it from anywhere. so, yes, it's unique. THANKS!

Uploaded by

Priya Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

24) To create DataFrame using dictionary.

import pandas as pd
d1=dict({'Name' : [ "Tony",'Natasha','Bruce','CarolD','Steve'],
'Power%': [80,91,99,99,80], 'Intelligence' : [100,80,90,82,60], 'Health' :
[92,90,91,94,95]})
df=pd.DataFrame(d1)
print(df)

*OUTPUT:
25) To use function ‘loc’ in the given DataFrame.

import pandas as pd
d1=dict({'Name' : [ "Tony",'Natasha','Bruce','CarolD','Steve'],
'Power%': [80,91,99,99,80], 'Intelligence' : [100,80,90,82,60],
'Health' : [92,90,91,94,95]})
df=pd.DataFrame(d1)
print(df)
print('')
print(' next' )
print('')
h=df.loc[: , 'Name' : 'Power%']
print(h)
*OUTPUT:
26) To add a new column in the givenDataFrame.

import pandas as pd

d1=dict({'Name' : [ "Tony",'Natasha','Bruce','CarolD','Steve'],
'Power%': [80,91,99,99,80], 'Intelligence' : [100,80,90,82,60],
'Health' : [92,90,91,94,95]})
df=pd.DataFrame(d1)
print(df)
print('adding a new column: ')
print('')
df['Capacity']= [99,80,90,100,85]
print(df)
*OUTPUT:
27)To give the maximum value of the given
DataFrame.

import pandas as pd
d1=dict({'Name' : [
"Tony",'Natasha','Bruce','CarolD','Steve'], 'Power%':
[80,91,99,99,80], 'Intelligence' : [100,80,90,82,60],
'Health' : [92,90,91,94,95]})
df=pd.DataFrame(d1)
print(df)
print(' to give the maximum value: ')
print(df.max(axis=1))
*OUTPUT:
28)To use the ‘mode’ function in the given
DataFrame.

import pandas as pd

d1=dict({'Name' : [
"Tony",'Natasha','Bruce','CarolD','Steve'], 'Power%':
[80,91,99,99,80], 'Intelligence' : [100,80,90,82,60],
'Health' : [92,90,91,94,95]})
df=pd.DataFrame(d1)
print(df)

print(df.mode(axis=0, numeric_only=False))
*OUTPUT:
29) To use the ‘Quantile’ function.

import pandas as pd

d1=dict({'Name' : [
"Tony",'Natasha','Bruce','CarolD','Steve'], 'Power%':
[80,91,99,99,80], 'Intelligence' : [100,80,90,82,60],
'Health' : [92,90,91,94,95]})
df=pd.DataFrame(d1)
print(df)

print(df.quantile(q=0.5, axis=0, numeric_only=True))


*OUTPUT:
Connecting Python with MySQL:

30) Creating a table in MySQL and then connecting it


with Python.

*Now, connecting the above table with python.


importmysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='tig
er',database='aman')
ifmycon.is_connected():
print('')
print('Succesfully Connected to MySQL database')
*OUTPUT:

*Now, to give commands:

(Please turn over)


31) To display all details in the above MySQL table.

importmysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='tiger',datab
ase='aman')
cur=mycon.cursor()
cur.execute('select * from movies')
for i in cur:
print(i)
cur.close()
32) To display details from the above MySQL table where
name starts with a.
importmysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='tiger',datab
ase='aman')
cur=mycon.cursor()
cur.execute("select * from movies where name like 'A%'")
for i in cur:
print(i)
cur.close()
33) To display names from above MySQL table those
have awards more than 10.
importmysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passw
d='tiger',database='aman')
cur=mycon.cursor()
cur.execute("select name from movies where
awards>10")
for i in cur:
print(i)
cur.close()
34) To add a new column in the above MySQL table.
importmysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='tig
er',database='aman')
cur=mycon.cursor()
cur.execute("alter table movies
addmonth_of_releasevarchar(10)")
for i in cur:
print(i)
mycon.commit()
cur.close()
35) To maximize the value of awards in the column
‘GodZilla’.
importmysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='tig
er',database='aman')
cur=mycon.cursor()
cur.execute("update movies set awards=awards+3 where
name='GodZilla'")
print('')
print('TABLE UPDATED SUCCESSFULLY!')

for i in cur:
print(i)
mycon.commit()
cur.close()
*OUTPUT:

You might also like