Wednesday, January 25, 2023

Python-DATAFRAME Basic Functions and Operations

Python DATAFRAME Basic Functions and Operations



CREATING DATAFRAME

 import pandas as pd

a= {'PNAME':['TV','AC','TV','WM','AC','TV'], 'COMP':['LG','WIRLPOOL','SONY','WIRLPOOL','LG','LG'], 'PRICE':[10000,25000,15000,12000,28000,15000],'QTY':[2,5,15,8,12,5]} df = pd.DataFrame(a,index=['P1','P2','P3','P4','P5','P6']) 
********************************************* FETCH SINGLE COLUMN

 print(df['PNAME'])ORprint(df.PNAME)
OUTPUT

 


 

 

 

 

 

********************************************* FETCH MULTIPLE COLUMNS

 print(df[['PNAME','PRICE']])

OUTPUT

 


 

 

 

 

 

 

 

********************************************* FETCH SINGLE ROW

 

 print(df.loc['P2'])
ORprint(df.iloc[1])

OUTPUT



  *********************************************
 FETCH MULTIPLE ROWS

 print(df.loc[['P2','P5']])

OUTPUT

 


 

 

 

*********************************************
 FETCH SOME ROWS, SOME COLUMNS

 print(df.loc[['P2','P5'],['PNAME','PRICE']])

OUTPUT


 

 

 

 

*********************************************
 FETCH SINGLE VALUE 
print(df['COMP']['P3'])

OUTPUT

 


  

*********************************************
 FETCH ALL VALUES HAVING QTY>5

 print(df[ (df['QTY']>5) ])

OUTPUT


 

 

 

 

*********************************************
 FETCH TOP 3 ROWS

print(df.head(3))

OUTPUT

 


 

 

 

 

 

*********************************************
 FETCH LAST 3 ROWS

 print(df.tail(3))

 OUTPUT

 


 

 

 

 

*********************************************
 ADDING NEW COLUMN 
df['DISCOUNT'] = [1,4,2,6,8,3]OR
df.loc[:,'DISCOUNT'] = [1,4,2,6,8,3] OR 
df.at[:,'DISCOUNT'] = [1,4,2,6,8,3]
 
 
 OUTPUT

 
 
 
 
 
********************************************* ADDING NEW ROW
df.loc['P10',:] = ['OS', 'ANDROID', 5000, 6]  OR df.at['P10',:] = ['OS', 'ANDROID', 5000, 6]
 OUTPUT
 

 
 
 
 
 
 
 
********************************************* DELETING A COLUMN  
 del df['COMP']  
OR df.drop(['COMP'],axis=1,inplace=True)  
OR df.drop(df.columns[1],axis=1,inplace=True)
 OUTPUT

 
 
 
 
 
 
 
 
 
 
 
 
 ********************************************* DELETING MULTIPLE COLUMNS
 df.drop(['PNAME','PRICE'],axis=1,inplace=True)
 OUTPUT

 
 
 
 
 
 
 
 
 
 
 
 
********************************************* DELETING A ROW
 
 df.drop(['P1'],axis=0,inplace=True)ORdf.drop(df.index[0],axis=0,inplace=True)
 OUTPUT

 
 
 
 
 
 
 
 
 
 
 
 
********************************************* DELETING MULTIPLE ROWS 
 df.drop(['P1','P2'],axis=0,inplace=True)
 
OUTPUT

 
 
 
 
 
 
 
 
 
********************************************* UPDATE COLUMN VALUES  
 df['PRICE'] = [1,2,3,4,5,6]
 

 
 
 
 
 
 
 
 
 
 
********************************************* UPDATE COLUMN WITH A SINGLE VALUE  
df['PRICE'] = 5

 
 
 
 
 
 
 
 
 
 
********************************************* UPDATE A ROW WITH A SINGLE VALUE 
 df.loc['P1',:] = 5
 

 
 
 
 
 
 
 
 
 
 
********************************************* UPDATE A ROW WITH DIFFERENT VALUES   
df.loc['P1',:] = [1,2,3,4]

 
 
 
 
 
 
 
 
 
 
********************************************* UPDATE A SPECIFIC RECORD 
 df['PRICE']['P1'] = 5
OR

df.loc['P1','PRICE'] = 5

OR

df.at['P1','PRICE'] = 5

 
 
 
 
 
 
 
 
 
 
********************************************* MISC FUNCTIONS
*********************************************
INCREASE VALUE OF QTY COLUMN BY 10  
df['QTY'] += 10

 
 
 
 
 
 
 
 
 
 
 
*********************************************SORT DATAFRAME BY QTY
 df.sort_values(axis=0,ascending=False,inplace=True,by=['QTY'])
 

 
 
 
 
 
 
 
 
 
 
*********************************************SUM OF QTY COLUMN
 print('SUM OF COLUMN IS = ', df['QTY'].sum())
 

 
 
 
 
 
 
*********************************************MINIMUM AND MAXIMUM VALUE FROM QTY COLUMN
print('MINIMUM QTY IS = ', df['QTY'].min())
print('MAXIMUM QTY IS = ', df['QTY'].max())

 
 
 
 
 
 
 
*********************************************MINIMUM AND MAXIMUM VALUE FROM EACH COLUMN 
print('MINIMUM IS = \n', df.min())
print('MAXIMUM IS = \n', df.max())
 

 
 
 
 
 
 
 
 
 
 
 
 
 
AS 
 

as

 

Friday, November 11, 2022

Python Practical List XI-IP

List of Practicals to be completed in Python for class XI IP

 

Instructions:

Prepare a Practical File.

Write all programs in right side lined page

write sample input and output in  left hand side blank page

write programs serially as per list of practicals

  
OLD FILES
 
 
 

 

Monday, August 1, 2022

Python CSV connectivity with no extra column added - Project Class XII CBSE

Important Python code to import and export dataframe to CSV file for Project Class XII CBSE

 

writing DataFrame to CSV File

data = {
  "name": ['RAM', 'SHYAM', 'SITA'],
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}
df = pd.DataFrame(data)
print(df)
df.to_csv("devendra_test.csv")

Output of print(df) statement 
 

This is how file will look like in CSV File   


Reading CSV File into Python DataFrame
df = pd.read_csv("devendra_test.csv")
print(df)

Output of print(df) statement 

Reading CSV File into Python DataFrame with no Extra Columns Added

df = pd.read_csv("devendra_test.csv",\
                 usecols=["name", "calories","duration"],\
                 dtype={"name": str, "calories":np.int32, "duration": np.float32})
print(df)
Again Output of print(df) statement 
 
 
 

Tuesday, October 13, 2020

Answer Key - CLASS XI - IP (UT-1 exam - 2020-21)

 

CLASS XI-IP

Answer Key - UT-1 exam  - 2020-21

NOTE – Theory answers can be searched in book.

 

Q. Convert 2.5GB into bytes                                                                                      

(2.5 x 1024 x 1024 x 1024) Bytes

GB -> MB -> KB -> Byte

 

Q.     Write output of following                                                                                    

i.                    print(“kv\nnepa”)

kv

nepa

 

ii.                  x, x=12,15

y, y=x+10,x+20

print(x, y)

 

x = 15, y = 35

 

iii.                print(4/2)

2.0

 

Q.     write name of function to find datatype of a variable x.                                                

type()

Q.     write a program to input a number form user and find its square.                               

num = int(input(“Enter a Number”))

sqr = num*num

print(“Square of “, num, ” is “, sqr)

 

Q.     Q. Convert into python expression                                                                                 

    Q  -     A

            (B+P)8

 

Q – A/((B+P)**8)

 or

Q - A/math.pow((B+P),8)

 

Q.     Write a program to obtain x, y, z from user and calculate 4x2 + 3y3 + 9z + 18  

import math

x = int(input(“Enter value of x”))

y = int(input(“Enter value of y”))

z = int(input(“Enter value of z”))

res = 4*(x**2) + 3*(y**3) + 9*z + 18*math.pi

print(res)

 

Q.      Given a list iterate it and display numbers which are divisible by 5 and if you find number greater than 150 stop the loop iteration                                                  

list1 = [12, 15, 32, 42, 55, 75, 122, 132, 150, 180, 200]

Expected output:

15

55

75

150

list1 = [12, 15, 32, 42, 55, 75, 122, 132, 150, 180, 200]

for i in list1:

    if(i>150):

        break

    if(i%5 == 0):

        print(i)

 

Q.     Q.  Write a program to Reverse the following list using for loop                                 

list1 = [10, 20, 30, 40, 50]

Expected output:

50

40

      30

20

10

list1 = [10, 20, 30, 40, 50]

for i in range(len(list1)-1, -1 , -1):

    print(list1[i])

 

Q.     Q. Write a program to Accept number from user and calculate the sum of all number between 1 and given number                                                                         

For example user given 10 so the output should be 55

 

num = int(input(“Enter a NUmber”))

sum = 0

for i in range(1,num+1):

    sum = sum + i

print(sum)