Saturday 20 February 2021

DataFrame in Pandas Python

 Data Frame

1. It is a data structure.

2. It is a 2D data structure.

3. It is hetrogeneous.

4. It is value mutable.

5. It is also size mutable.


Syntax to create a Data Frame:


import pandas as pd



dataframe_object=pd.DataFrame(a 2D data structure,[columns=<column_sequence>],[index=<index Sequence>])


How to create DataFrame

1. Using 2D Dictionary

2. Using 2D ndarray

3. Using Series Object

4. Using another DataFrame Object


#DataFrame using 2D Dictionary

import pandas as pd

dic={'Students':['Ram','Raj','Sam','John'],'Marks':[40,20,35,28]}

D=pd.DataFrame(dic)

print(D)



import pandas as pd

dict1={'Section':['A','B','C','D'],'Contri':[6700,5600,5000,5200]}

D=pd.DataFrame(dict1)

print(D)

Friday 19 February 2021

Python String Method


>>> S="Hello"

>>> S.upper()

'HELLO'

>>> S.lower()

'hello'

>>> len(S)

5

>>> S="hello india"

>>> S.capitalize()

'Hello india'

>>> S.isspace()

False

>>> S=" "

>>> S.isspace()

True

>>> S="hello india"

>>> len(S)

11

>>> S=""

>>> S.isspace()

False

>>> S=" "

>>> S.isspace()

True

>>> S="        "

>>> S.isspace()

True

>>> S="A     "

>>> S.isspace()

False

>>> S=""

>>> S.isalnum()

False

>>> S=" "

>>> S.isalnum()

False

>>> S="123"

>>> S.isalnum()

True

>>> S="abc123"

>>> S.isalnum()

True

>>> S="a$2"

>>> S.isalnum()

False

>>> S="Ram Kumar"

>>> S.isalnum()

False

>>> S="abc"

>>> S.isalnum()

True

>>> S=""

>>> S.isalpha()

False

>>> S1="123"

>>> S1.isalpha()

False

>>> S2="abc"

>>> S2.isalpha()

True

>>> S3="abc123"

>>> S3.isalpha()

False

>>> S4="12"

>>> S4.isalnum()

True

>>> S5="1.2"

>>> S5.isalnum()

False

>>> S="Ram Kumar Sharma"

>>> B="Kumar"

>>> S.find(B)

4

>>> C="kumar"

>>> S.find(C)

-1

>>> D="m"

>>> S.find(D)

2

>>> S.find(D,3)

6

>>> S.find(D,6,14)

6

>>> S.find(D,7,14)

-1

>>> len(S)

16

>>> S.find(D,7)

14

>>> S.find(D,S.find(D)+1)

6

>>> A="   Ram"

>>> print(A)

   Ram

>>> print(A.lstrip())

Ram

>>> A

'   Ram'

>>> A.lstrip()

'Ram'

>>> A="AAAARam"

>>> A.lstrip('A')

'Ram'

>>> A="BBBBHello"

>>> A.lstrip('B')

'Hello'

>>> A="Amarjeet Kumar"

>>> A

'Amarjeet Kumar'

>>> A.lstrip()

'Amarjeet Kumar'

>>> A.lstrip('Amar')

'jeet Kumar'

>>> A="Ram      "

>>> A.rstrip()

'Ram'

>>> A="Amarjeet Kumar"

>>> A.rstrip(' Kumar')

'Amarjeet'

>>> A='k'

>>> ord(A)

107

>>> A='B'

>>> ord(A)

66

>>> chr(65)

'A'

>>> chr(97)

'a'

>>> A="Hello India"

>>> A.swapcase()

'hELLO iNDIA'

>>> A="S P SHARMA CLASSES"

>>> A.partition(' ')

('S', ' ', 'P SHARMA CLASSES')

>>> A="IndiaHelloDelhi"

>>> A.partition('Hello')

('India', 'Hello', 'Delhi')

>>> A="   Ram    "

>>> A.strip()

'Ram'

>>> A="S P SHARMA CLASSES"

>>> A.partition('P ')

('S ', 'P ', 'SHARMA CLASSES')


Thursday 18 February 2021

Data File Handling in Python

 

f=open("a.txt","r")

'''print("File Name=",f.name)

print("File Mode=",f.mode)

print("Is File Readable=",f.readable())

print("Is File Closed=",f.closed)

f.close()

print("Is File Closed=",f.closed)


print(f.read())

print(f.read(16))

f.read(18)

print(f.read(12))


print(f.readline(),end="")

print(f.readline(),end="")

print(f.readline(),end="")

print(f.readline(),end="")

print(f.readline(),end="")

'''

data=f.readlines()

for i in data:

    print(i,end="")

f.close()







import sys
'''f=open("ankur.txt","w")
print("Enter the content of file:\n ")
data=sys.stdin.readlines()
f.writelines(data)
print("File Created")
f.close()'''
print("The content of file:\n ")
f=open("ankur.txt","r")
print("Before Reading=",f.tell())
f.seek(10)
data=f.read()
print(data)
print("After Reading=",f.tell())
f.seek(20,0)
print(f.read())

f.close()








import pickle
f=open("ankur1.dat","wb")
data="Hello India"
pickle.dump(data,f)
f.close()
print("File create")
f=open("ankur1.dat","rb")
data=pickle.load(f)
print(data)
f.close()

Wednesday 17 February 2021

Lambda Function in Python

 '''

#Function as an object

def sum(a,b):

    return a+b


#k=sum(5,9)

#print(k)


k=sum

print(k(5,10))

print(k(15,10))



def show():

    print("Hello")



m=show

m()

m()

show()




Anonymous or Lambda Function

'''


print((lambda x:2*x)(5))


k=lambda x:x**2

print(k(5))


m=lambda x,y:x+y

print(m(5,10))


Types of arguments in Python

 #Positional Arguments


'''

def show(a,b):

    print(a-b)



show(5,2)

show(2,5)

#show(5)

#show(5,2,6)



#Default Arguments

def show1(a,b=2):

    print(a-b)


show1(5)

show1(5,3)



def show2(a=10,b=5):

    print(a-b)


show2()

show2(5)

show2(5,3)


def show3(a=10,b):

    print(a-b)


#show3()

#show3(5)

show3(5,3)


Keyword arguments/Named Arguments


def display(a,b):

    print(a-b)


display(a=5,b=2)

display(b=2,a=5)

#display(a=5,2)

display(5,b=2)

#display(b=2,5)

display(5,2)



Variable Length Arguments

'''


def sum(*n):

    s=0

    for i in n:

        s=s+i

    print("Sum=",s)



sum()

sum(5)

sum(5,10)

sum(5,10,15)


4 - MySql Practical for class 12 CS and IP

 Write the sort notes on group by, having and where clause with example.


WHERE Clause:

WHERE Clause is used to filter the records/rows from the table. It can be used with SELECT, UPDATE, DELETE statements.

Select * from employees where salary>25000;


Group by Clause:

The GROUP BY clause is used with aggregate functions (MAX, SUM, AVG, COUNT, MIN) to group the results by one or more columns i.e. The GROUP BY clause is used with the SELECT statement to arrange required data into groups. 


The GROUP BY statement groups rows that have the same values. This Statement is used after the where clause. This statement is often used with some aggregate function like SUM, AVG, and COUNT etc. to group the results by one or more columns.

SELECT COUNT (City) AS COUNT_CITIES, City FROM EMPLOYEES GROUP BY City;


Having Clause:


Having Clause is like the aggregate function with the GROUP BY clause. The HAVING clause is used instead of WHERE with aggregate functions. While the GROUP BY Clause group rows that have the same values into rows. The having clause is used with the where clause in order to find rows with certain conditions. The having clause is always used after the group by clause.

SELECT COUNT (City) AS COUNT_CITIES, City FROM EMPLOYEES

GROUP BY City HAVING COUNT (City) > 1;


WHERE Clause

Having Clause

Group By Clause

WHERE Clause is used to filter the records from the table based on the specified condition.

HAVING Clause is used to filter record from the groups based on the specified condition.

The group by clause is used to group the data according to particular column or row.

WHERE Clause can be used without GROUP BY Clause

HAVING Clause cannot be used without GROUP BY Clause

Group by can be used without having clause with the select statement.

WHERE Clause implements in row operations

HAVING Clause implements in column operation

It groups the output on basis of some rows or columns.

WHERE Clause cannot contain aggregate function

The having clause can contain aggregate functions.

It cannot contain aggregate functions.

WHERE Clause is used with single row function like UPPER, LOWER etc.

HAVING Clause is used with multiple row function like SUM, COUNT etc

Group by Clause is a multiple row function

WHERE Clause can be used with SELECT, UPDATE, DELETE statement.

HAVING Clause can only be used with SELECT statement.

The GROUP BY clause is used in the SELECT statement.

WHERE Clause is used before GROUP BY Clause

HAVING Clause is used after GROUP BY Clause

Group By is used after the Where clause and before the HAVING Clause


3- Mysql Practical for class 12 CS and IP

 

 Write the difference between drop, delete and truncate command with example.

Drop:

Drop is a DDL (Data Definition Language) command of SQL.

Drop is used to remove an object completely just like remove database, table, view, index etc.

Syntax:

DROP object object_name

Examples:

DROP TABLE table_name;

table_name: Name of the table to be deleted.

DROP DATABASE database_name;

database_name: Name of the database to be deleted.


Delete:

Delete is a DML (Data Manipulation Language) command of SQL.

Delete is used to remove one or more rows from a table, view, index etc. We can use where clause with delete command. Delete remove one row at a time i.e. it remove rows one by one. It will not delete columns or structure of table.

Syntax:

delete from table_name where condition;

Examples:

delete from table_name;

table_name: Name of the table from which rows will be deleted.

Delete all rows from the table.

delete from table_name where condition;

delete specific rows on the bases of where clause condition.


Truncate:

Truncate is a DDL (Data Defination Language) command of SQL.

Truncate is used to remove all rows from a table, view, index etc. We can not use where clause with truncate command. Truncate remove all rows at once, but it will not delete structure of the table or any object.

Syntax:

delete from table_name where condition;

Examples:

trucate table_name;

table_name: Name of the table from which rows will be deleted.

Delete all rows from the table at once.


Tuesday 16 February 2021

11 - MySql Practical for class 12 CS and IP

 

11. Write a SQL to Enter 5 Employee data in a single query in the table Emp (eid, ename, salary, city, dob).











10 - MySql Practical for class 12 CS and IP

 

10. Write the SQL Query for full outer join of two tables Emp(eid, ename, salary,dept_id) and Dept(dept_id, dname)



 



9 - MySql Practical for class 12 CS and IP

 

9. Write the SQL Query for inner join of two tables Emp(eid, ename, salary,dept_id) and Dept(dept_id, dname)