IP grade 12 record
IP grade 12 record
import pandas as pd
dict1={“Rahul”:85,”Arjun”:90,”Ankitha”:95}
s1=pd.Series(dict1)
print(s1)
import pandas as pd
s1=pd.Series([11,22,33,44,55])
print(“series object1:”)
print(s1)
import pandas as pd
s1=pd.Series(5000,index=[‘q1’,’q2’,’q3’,’q4’])
print(s1)
import pandas as pd
import numpy as np
nda1=np.arange(3,13,3.5)
print(nda1)
s1=pd.Series(nda1)
print(s1)
s2=pd.Series(np.linspace(20,50,4))
print(s2)
s3=pd.Series(np.tile([5,6],3))
print(s3)
5. Write a series program to print all the elements that are above the
th
75 percentile.
import pandas as pd
import numpy as np
s=pd.Series(np.array([1,2,3,4,5,6,7,8,9,10]))
print(s)
ans=s.quantile(q=0.75)
print()
print('75th percentile of series=')
print(ans)
print()
print(s[s>ans])
import pandas as pd
s1=pd.Series(data=[9,8,7,6,5])
s2=pd.Series(data=[2,3,4,5])
print("sum")
print(s1+s2)
print("difference")
print(s1-s2)
print("product")
print(s1*s2)
print("division")
print(s1/s2)
import numpy as np
import pandas as pd
x=pd.Series(data=[2,4,6,8])
y=pd.Series(data=[11,22,33,44,np.NaN],index=['a','b','c','d','e'])
print(x.index)
print(x.values)
print(y.index)
print(y.values)
print(x.shape)
print(y.shape)
print(x.ndim)
print(y.ndim)
print(x.size)
print(y.size)
print(x.hasnans)
print(y.hasnans)
8. Write a Pandas program to join the two given dataframes along rows
and assign all data.
import pandas as pd
stdata1=pd.DataFrame({'stid':['S1','S2','S3','S4','S5'],
'name':['Deepthi','Rohit','Sanvi','Tarun','Kavya'],
'marks':[200, 210, 190, 222, 199]})
stdata2=pd.DataFrame({'stid':['S4','S5','S6','S7','S8'],
'name':['Arnav','Chaitanya','Gargi','Sathvik','Manvi'],
'marks':[201, 200, 198, 219, 201]})
print("Source DataFrames:")
print(stdata1)
print("----------------------------")
print(stdata2)
print("\ntwo dataframes after joining along rows:")
result=pd.concat([stdata1,stdata2])
print(result)
import pandas as pd
stdata1 = pd.DataFrame({
'stid': ['S1','S2','S3','S4','S5'],
'name': ['Harini','Riyan','Bhanu','Akash','Sarayu'],
'marks': [200, 210, 190, 222, 199]})
s6=pd.Series(['S6','Kevin', 205],index=['stid','name','marks'])
dicts= [{'stid': 'S6', 'name': 'Kevin', 'marks': 203},
{'stid': 'S7', 'name': 'Bhanu', 'marks': 207}]
print("Main DataFrames:")
print(stdata1)
print("\nDictionary:")
print(s6)
joineddata=stdata1._append(dicts,ignore_index=True,sort=False)
print("\njoinedData:")
print(joineddata)
import pandas as pd
import numpy as np
s1=pd.Series([80,90,100,100],index=['eng','sci','maths','IT'])
s2=pd.Series([99,np.NaN,98,90],index=['eng','sci','maths','IT'])
s3=pd.Series([np.NaN,90,99,96],index=['eng','sci','maths','IT'])
s4=pd.Series([99,89,100,99],index=['eng','sci','maths','IT'])
s5=pd.Series([98,96,np.NaN,100],index=['eng','sci','maths','IT'])
df=pd.DataFrame({'grade10':s1,'grade9':s2,'grade11':s3,'grade4':s4,'
grade10A':s5})
print("source dataframe")
print(df)
print("change the name from grade10 to Tenth")
df.rename(columns={'grade10':"tenth"},inplace=True)
print(df)
print("counting and displaying the non-NaN values in columns")
print(df.count())
print("counting and displaying the non-NaN values in rows")
print(df.count(1))
print("Increasing the marks in english by adding one mark")
df.loc['eng',:]=df.loc['eng',:]+1
print(df)
print("change all the NaN values with zero")
df.fillna(0,inplace=True)
print(df)
import pandas as pd
emp={'Ename':['Heera','Aadhya','Bharath','Naren','Chatur'],
'Department':['Sales','Accounts','Sales','HR','Accounts'],
'Salary':[20000,25000,22000,30000,25000]}
df=pd.DataFrame(emp,index=['E012','E015','E021','E026','E051'])
print(df.loc[df['Salary']>24000])
12. Write a python program which uses descriptive statistics with pandas.
import pandas as pd
import numpy as np
dict1={'fruits':[7830.0,11950.0,113.0,7152.0,44.1,140169.2],
'pulses':[931.0,818.0,1.7,33.0,23.2,2184.4],
'rice':[7452.4,1930.0,2604.8,11586.2,814.6,13754.0],
'wheat':[np.NaN,2737.0,np.NaN,16440.5,0.5,30056.0]}
df1=pd.DataFrame(dict1,index=['Andhra','Gujarat','kerala','Punjab','
Tripura','UttarP'])
print(df1)
print(df1.mode())
print(df1.mean())
print(df1.median())
print(df1.quantile([.25,.5,.75,1]))
print(df1.describe())
print(df1.info())
print(df1.head())
print(df1.tail())
import pandas as pd
emp={'Ename':['Heera','Aadhya','Naren','Chatur'],
'Department':['Sales','Accounts','Sales','HR'],
'area':['lane1','lane2','lane3','lane4'],
'Salary':[20000,25000,22000,30000]}
df=pd.DataFrame(emp,index=['E012','E015','E021','E026'])
df.to_csv("C:\\Users\\Lenovo\\Desktop\\emp.csv")
data=pd.read_csv("C:\\Users\\Lenovo\\Desktop\\emp.csv")
print(data)
14. Write a python program to show the connectivity between MySQL and
Python Dataframe.
import pandas as pd
import mysql.connector as sqltor
mycon=sqltor.connect(host="localhost",user="root",passwd='1234',
database='vahila1')
if mycon.is_connected():
df1=pd.read_sql("select * from student;",mycon)
print(df1)
else:
print("connection problem")
15. Write a python program to display the data of a data frame row wise
and column wise using iterrows() and items().
import pandas as pd
d={'sname':['Krithi','Arjun','Chaitanya','Naithik','Geethu'],
'qualification':['MCA','MBA','Bsc','B.tech','M.E'],
'percentage':[95,96,80,85,99]}
df=pd.DataFrame(d,index=['st1','st2','st3','st4','st5'])
for (row,values) in df.iterrows():
print(row)
print(values)
print()
for (col,values) in df.items():
print(col)
print(values)
print()
16. Write a python program to plot a line chart based on the given data.
Also, give appropriate axes labels, title and keep marker style as
diamond and marker edge color as ‘red’ for product1.
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'subject':['eng','phy','maths','chem','IP'],
'sub-avg':[72,85,78,92,80]},
index=['sub1','sub2','sub3','sub4','sub5'])
df.to_csv("C:\\Users\\Lenovo\\Desktop\\new.csv")
data=pd.read_csv("C:\\Users\\Lenovo\\Desktop\\new.csv")
plt.title("subject analysis report")
plt.xlabel("subjects")
plt.ylabel("average")
plt.bar(data['subject'],data['sub-avg'],
width=0.50,color=['c','r','g','b','m'])
plt.show()
18. To write a Python program to plot a multiple bar chart From CSV file
using Matplotlib for subject wise Scores of Class A, Class B, and
Class C. Different colors represent each class, and subjects include
English, Accountancy, Economics, BST and IP. Proper labels,
a title and a legend are displayed on the chart.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data={'subject':['eng','acc','eco','bst','IP'],
'classA':[85,78,92,80,98],
'classB':[90,65,88,75,87],
'classC':[75,82,60,90,94]}
df=pd.DataFrame(data)
df.to_csv("C:\\Users\\Lenovo\\Desktop\\new1.csv")
new=pd.read_csv("C:\\Users\\Lenovo\\Desktop\\new1.csv")
plt.xlabel('subjects')
plt.ylabel('average')
plt.title("subjectwise analysis for classes A, B, C")
x=np.arange(5)
plt.bar(df['subject'],df['classA'],width=0.25,label='class--A')
plt.bar(x+0.15,df['classB'],width=0.25,label='class--B')
plt.bar(x+0.30,df['classC'],width=0.25,label='class--C')
plt.legend()
plt.show()
show databases;
use employee;
show tables;
desc empl;
2. To write Queries for the following Questions based on the given table:
c. Write a Query to get the name and salary of the employee whose salary is
above 1500 and job is not president.
select ename,sal from empl where sal>1500 and job<>'president';
Table: student
e. Write a query to display the names of the students who joined in the
month of June.
h. Write a query to display the names of all students and extract five
characters from the third position of the 'Name' field.
(or)
select * from student s join sinfo i on(s.Rollno=i.Rollno);
iv. INTERSECTION
select name from vendor intersect select name from customer;