Python Lab Experiments: Bachelor of Technology
Python Lab Experiments: Bachelor of Technology
Bachelor of Technology
By
HARSH SHUKLA
0901IO211023
Under the Supervision of
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'bool'>
<class 'NoneType'>
<class 'str'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'set'>
<class 'range'>
<class 'frozenset'>
<class 'bytes'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>
BY HARSH SHUKLA
0901IO211023
Normal Addition = 25
Normal Subtraction = 15
Multiplication = 100
Division = 4.0
remainder = 0
Power = 25
whole division = 4
BY HARSH SHUKLA
0901IO211023
#Slicing of strings
print(a1[1:3])
print(a1[:3])
print(a1[:])#no parameter concept
print(a1[::-1])#reverse string concept
print(a1[0:8])#outof bound concept
print(a1[8:6])#No OUTPUT
print(a1[1:2:2])
print(a1[-6:-1:1])#printing the original name by using negative indexing
print(a1[-9:-9])#no output(empty output)
print(a1[-5:3])
print("BY HARSH SHUKLA")
print("0901IO211023")
BY HARSH SHUKLA
0901IO211023
BY HARSH SHUKLA
0901IO211023
BY HARSH SHUKLA
0901IO211023
{'key': 'harsh'}
{'name': 'harsh', 'branch': 'ITIOT', 'subject': 'DS'}
{123: 'harsh', True: 123}
harsh
123
123
Akanchha Maam
english
{'name': ['harsh', 'shukla', 'ji'], 'branch': ('ITIOT', 'CSE', 'IT+CSE'), 'Subject no': {101, 102, 103}, 'teacherName':
{'ITIOT': 'Kritika maam', 'CSE': 'Pawan sir ', 'IT+CSE': 'Aku Maam'}}
{'name': ['harsh', 'shukla', 'ji'], 'branch': ('ITIOT', 'CSE', 'IT+CSE'), 'Subject no': {101, 102, 103}, 'teacherName':
{'ITIOT': 'Kritika maam', 'CSE': 'Pawan sir ', 'IT+CSE': 'Aku Maam'}, 'Makrs': [19, 20, 18]}
{'branch': ('ITIOT', 'CSE', 'IT+CSE'), 'Subject no': {101, 102, 103}, 'teacherName': {'ITIOT': 'Kritika maam', 'CSE':
'Pawan sir ', 'IT+CSE': 'Aku Maam'}, 'Makrs': [19, 20, 18]}
dict_keys(['branch', 'Subject no', 'teacherName', 'Makrs'])
dict_values([('ITIOT', 'CSE', 'IT+CSE'), {101, 102, 103}, {'ITIOT': 'Kritika maam', 'CSE': 'Pawan sir ', 'IT+CSE': 'Aku
Maam'}, [19, 20, 18]])
dict_items([('branch', ('ITIOT', 'CSE', 'IT+CSE')), ('Subject no', {101, 102, 103}), ('teacherName', {'ITIOT': 'Kritika
maam', 'CSE': 'Pawan sir ', 'IT+CSE': 'Aku Maam'}), ('Makrs', [19, 20, 18])])
BY HARSH SHUKLA
0901IO211023
#Q7-> Write a python program to find the factorial of a number using recursion
#BY HARSH SHUKLA(0901IO211023)
# Factorial of a number using recursion
def fact(n):
if n == 1:
return 1
else:
return n*fact(n-1)
def returns(self):
return self.num
class Swap:
def Swaps(self, num1, num2): #here we use object as input
c = num1.returns()
d = num2.returns()
c= c + d
d= c - d
c=c-d
return c,d
a = int(input("Enter 1st Number : "))
b = int(input("Enter 2nd Number : "))
num1 = inputs(a) #we make two objects num1 and num2
num2 = inputs(b)
print("Varibles before swapping : ",a,b)
obj_Swap = Swap()
print("Variables after swapping :" , obj_Swap.Swaps(num1, num2))
print("BY HARSH SHUKLA")
print('0901IO211023')
BY HARSH SHUKLA
0901IO211023
#1ST Method for finding Greater of two numbers in two different classes
#BY HARSH SHUKLA(0901IO211023)
class inputs:
def __init__(self,a,b):
self.a = a
self.b = b
def compare_Num(self):
if(self.a > self.b):
return str(self.a)+" "+"is greater then "+str(self.b)
else:
return str(self.b) + " " + "is greater then " + str(self.a)
a = int(input("Enter 1st Number : "))
b = int(input("Enter 2nd Number : "))
obj_inp = inputs(a,b)
print(obj_inp.compare_Num())
print("BY HARSH SHUKLA")
print('0901IO211023')
BY HARSH SHUKLA
0901IO211023
#2nd Method for finding Greater of two numbers in two different classes
#BY HARSH SHUKLA(0901IO211023)
class inputs:
def __init__(self, number):
self.num = number
def returns(self):
return self.num
class Compares:
def compare(self, num1, num2): #here we use object as input
if num1.returns() > num2.returns():
return str(num1.returns()) +" "+"is greater then "+str(num2.returns())
else:
return str(num2.returns()) +" "+"is greater then "+str(num1.returns())
a = int(input("Enter 1st Number : "))
b = int(input("Enter 2nd Number : "))
num1 = inputs(a) #we make two objects num1 and num2
num2 =inputs(b)
obj_Compares = Compares()
print(obj_Compares.compare(num1, num2))
BY HARSH SHUKLA
0901IO211023
#Q10 -> Write a python program to define a module and import a specific function
#in that module to another program
#BY HARSH SHUKLA (0901IO211023)
Calculator.py (Module)
def add(a,b):
return a+b
def sub(a,b):
return a-b
def multiply(a,b):
return a*b
def divide(a,b):
return a/b
main1.py(Module)
6
-4
90
18.0
BY HARSH SHUKLA
0901IO211023
a=1
print(type(a))
a = 1.3
print(type(a))
a = 1+2j
print(type(a))
a = True
print(type(a))
a = 'a'
print(type(a))
a = "Harsh";
print("BY HARSH SHUKLA")
print("0901IO211023")
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'bool'>
<class 'str'>
<class 'str'>
BY HARSH SHUKLA
0901IO211023
#demonstarting id()
var = 60
print(id(var)) #id function mainly discribes the address(unique id) of varibale
#demonstrarting type()
var1 = 'Harsh'
print(type(var1))
#type() describes the data type of the variable
var2 = 1+2j
print(type(var2))
#demonstarting range()
lst = []
lst = list(range(0,10)) #it give the number specifed from start , end - 1
print(lst)
print("BY HARSH SHUKLA")
print("0901IO211023")
OUTPUT OF 2ND MICRO PROGRAM :
2241509066832
<class 'str'>
<class 'complex'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
BY HARSH SHUKLA
0901IO211023
def binary_to_decimal(binary_num):
"""Converts binary number to decimal."""
return int(binary_num, 2)
def decimal_to_hex(decimal_num):
"""Converts decimal number to hexadecimal."""
return hex(decimal_num)
def hex_to_decimal(hex_num):
"""Converts hexadecimal number to decimal."""
return int(hex_num, 16)
# Examples
print(decimal_to_binary(9))
print(binary_to_decimal('0b1000'))
print(decimal_to_hex(189))
print(hex_to_decimal('0xfe'))
print("BY HARSH SHUKLA")
print("0901IO211023")
0b1001
8
0xbd
254
BY HARSH SHUKLA
0901IO211023
# Examples
print(num1)
print(num2)
print(float1)
print(float2)
print(str1)
print(str2)
print(str3)
print(str4)
print(float3)
print(num3)
print("BY HARSH SHUKLA ")
print("0901IO211023")
10
10
3.14
0.00271828
42
10
3.14159
0.00271828
42.0
3
BY HARSH SHUKLA
0901IO211023
30
-10
200
0.5
10
100
0
BY HARSH SHUKLA
0901IO211023
INCREMENT/DECREMENT OPERATORS
''' = , += , -='''
#here ++ , -- (increment or decrement operator doesn't work)
a = 20
print(a)
a+=5 #a=a+5
print(a)
a-=5 #a = a - 5
print(a)
print("BY HARSH SHUKLA ")
print("0901IO211023")
20
25
20
BY HARSH SHUKLA
0901IO211023
BITWISE OPERATORS
'''& , | , ^'''
a = 10
b=8
print(a&b) #with bin() we get number in numeric form
print(bin(a)) # bin() is a function to get the binary form of a number in 5 bit form
print(bin(b))
print(bin(a&b)) # ans would be in binary formate becuase we write bin() to convert ans
COMPARISON OPERATORS
BY HARSH SHUKLA
0901IO211023
IDENTITY OPERATORS
True
True
True
True
True
True
False
False
BY HARSH SHUKLA
0901IO211023
LOGICAL OPERATORS
MEMBERSHIP OPERATORS
#input()
Name = input("Enter your Name : ")
#print()
print("Name Entered : ",Name)
#'sep' attribute
print("This is very costly",sep='!')
#'end' attribute
#replacement operator
name = "Harsh"
age = 18
#if construct
age = 20
marks = 75
#while loop
count = 0
#for loop
fruits = ["apple", "banana", "cherry", "orange"]
0
1
2
3
4
apple
banana
cherry
orange
BY HARSH SHUKLA
0901IO211023
#break
numbers = [1, 2, 3, 4, 5]
print("Loop exited.")
#continue
numbers = [1, 2, 3, 4, 5]
print("Loop exited.")
#pass
def hello():
pass
print("BY HARSH SHUKLA")
print("0901IO211023")
1
2
Loop exited.
1
2
4
5
Loop exited.
BY HARSH SHUKLA
0901IO211023
LIST OF MACRO PROGRAMS
# Accessing the second last character of the string using negative indexing
print("Second last character using negative indexing:", my_string[-2])
#String Slicing
st = "Harsh Shukla"
print(st[:3] + st[3:])
print(st[:3],st[3:])
print(st[::-1])
print(st[:-5] + st[-5:])
print(st[11:18])
print(st[12:21])
print("BY HARSH SHUKLA")
print("0901IO211023")
BY HARSH SHUKLA
0901IO211023
# len()
print("Length of the string:", len(my_string))
# strip()
print("String after stripping whitespace:", my_string.strip())
# rstrip()
print("String after stripping whitespace from the right:", my_string.rstrip())
# lstrip()
print("String after stripping whitespace from the left:", my_string.lstrip())
# find()
print("Index of 'World' in the string:", my_string.find("World"))
# rfind()
print("Index of last 'o' in the string:", my_string.rfind("o"))
# index()
print("Index of 'World' in the string:", my_string.index("World"))
# rindex()
print("Index of last 'o' in the string:", my_string.rindex("o"))
# count()
print("Number of 'l' characters in the string:", my_string.count("l"))
# replace()
print("String after replacing 'World' with 'Universe':", my_string.replace("World", "Universe"))
# split()
print("List of words in the string:", my_string.split())
# join()
list_of_words = ["Hello", "World"]
print("String after joining the list of words:", " ".join(list_of_words))
# upper()
print("Uppercase version of the string:", my_string.upper())
# lower()
print("Lowercase version of the string:", my_string.lower())
# swapcase()
print("String with swapped case:", my_string.swapcase())
# title()
print("Titlecased version of the string:", my_string.title())
# capitalize()
print("Capitalized version of the string:", my_string.capitalize())
# startswith()
print("Does the string start with 'Hello'?", my_string.startswith("Hello"))
# endswith()
print("Does the string end with 'World!'?", my_string.endswith("World!"))
print("BY HARSH SHUKLA")
print("0901IO211023")
BY HARSH SHUKLA
0901IO211023
MACRO PROGRAM 3RD
'''Examples of map() , reduce() , and filter() and using map() , reduce() and filter() with lambda function'''
#BY HARSH SHUKLA (0901IO211023)
#map()
l = [1,2,3,4,5] #square of each element of list
def sqr(l):
l1 = []
for i in l:
l1.append(i**2)
return l1
print(sqr(l))
#reduce()
from functools import reduce
ll = [1,2,3,4,5,6]
print(reduce(lambda x,y : x + y , ll))
#filter()
#to find even number from list
print(list(filter(lambda x : x%2==0 , ll)) )#even number
[1 2 3 4 5]
<class 'numpy.ndarray'>
1
[[1 2 3]
[4 5 6]]
2
5
6
(5,)
(2, 3)
int32
int32
BY HARSH SHUKLA
0901IO211023
#so agr mujhe numpy me empty matrix create karna hai toh iske liye hum empty() ka istemaal karenge but
empty matrix kuch nhi
#hota hai numpy me agr me empty() ka istemaal karta hun toh voh random value generate kar dega.........
Arr_emp = np.empty((3,5))
print(Arr_emp)
print("BY HARSH SHUKLA")
print("0901IO211023")
[[1 1 1]
[1 1 1]
[1 1 1]]
[1. 1. 1. 1. 1.]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]]
[[False False False False False False False False]
[False False False False False False False False]
[False False False False False False False False]
[False False False False False False False False]
[False False False False False False False False]]
[[6.23042070e-307 3.56043053e-307 1.60219306e-306 2.44763557e-307
1.69119330e-306]
[1.78022342e-306 1.05700345e-307 1.24610383e-306 8.45590539e-307
9.34607074e-307]
[6.89805151e-307 1.78020169e-306 1.42410974e-306 3.23796840e-317
2.02369289e-320]]
BY HARSH SHUKLA
0901IO211023
#linspace()
#is function me hame ek range deni padti hai or us range me humko kitne numbers chahiye voh bhee batana
hota hai
#or jo numbers ate hai voh random ate hai us given range me jo exits karte hai
arr_2 = np.linspace(1,6,5)
#iska mutlb 1 se 6 ke beech me mujhe 5 numbers chahiye voh 5 numbers kuch bhee ho skte hai
print(arr_2)
#reshape()
#important function is respace function hai because iske through hum 1-D array ko 2-D , or 3-D array me
convert kar skte hai
#Conversion of 1-D array to 2-D array
arr_3 = arr.reshape(3,4) #(row,column)
#reshape function me dhiyan ye rakhna hai ki jo product hoga row * column ka voh hamare total no of
elements ke equal hona
#chahiye
print(arr_3)
#Coversion of 1-D array to 3-D array
arr_4 = arr.reshape(3,2,2)#(3-D array kitne parts me divide hoga,row,column)
#sub ka multiplication 12 ke equal hona chahiye
print(arr_4)
#now using both arange() and reshape function we can create a 2-D array
arrd = np.arange(1,13).reshape(2,6)
print(arrd)
#ravel function
#agr hame 2-d ya 3-D array ko 1-D me convert karna hai toh iske liye hum reval function ka istemaal karenge
ar = arrd.ravel()
print(ar)
#flatten function
#flatten function me bhee hum 2-d ya 3-d array ko 1-D me convert kar skte hai but phir ravel or flatten me
difference kiya hai
#so flatten me hum ek paramter bhee de skte hai or mujhe iske bare me jankari nhi dhekna padega
are = arrd.flatten()
print(are)
#transpose function
#transpose function ke through hum rows ko column me interchange kar skte hai or vise versa
#ex-> (2,6) so it will change to (6,2)
ares = arrd.transpose()
print(ares)
[ 1 2 3 4 5 6 7 8 9 10 11 12]
[ 0 2 4 6 8 10 12]
[1. 2.25 3.5 4.75 6. ]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]]
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
[ 1 2 3 4 5 6 7 8 9 10 11 12]
[ 1 2 3 4 5 6 7 8 9 10 11 12]
[[ 1 7]
[ 2 8]
[ 3 9]
[ 4 10]
[ 5 11]
[ 6 12]]
[[ 1 7]
[ 2 8]
[ 3 9]
[ 4 10]
[ 5 11]
[ 6 12]]
BY HARSH SHUKLA
0901IO211023
#agr hame har row/column me maximum value find karni hai toh iske liye hum max(axis) parameter denge
#for axis = 0 -> for column
#for axis = 1 -> for row
print(arr_1.max(axis = 0))
print(arr_1.max(axis = 1))
#if we have to find the square root of each and every element of matrix we use sqrt function
print(np.sqrt(arr_1))
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 2 4 6 8]
[10 12 14 16]
[18 20 22 24]]
[[ 2 4 6 8]
[10 12 14 16]
[18 20 22 24]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[ 1 4 9 16]
[ 25 36 49 64]
[ 81 100 121 144]]
[[ 1 4 9 16]
[ 25 36 49 64]
[ 81 100 121 144]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
12
11
[ 9 10 11 12]
[ 4 8 12]
78
[15 18 21 24]
[10 26 42]
6.5
[[1. 1.41421356 1.73205081 2. ]
[2.23606798 2.44948974 2.64575131 2.82842712]
[3. 3.16227766 3.31662479 3.46410162]]
3.452052529534663
[[2.71828183e+00 7.38905610e+00 2.00855369e+01 5.45981500e+01]
[1.48413159e+02 4.03428793e+02 1.09663316e+03 2.98095799e+03]
[8.10308393e+03 2.20264658e+04 5.98741417e+04 1.62754791e+05]]
[[0. 0.69314718 1.09861229 1.38629436]
[1.60943791 1.79175947 1.94591015 2.07944154]
[2.19722458 2.30258509 2.39789527 2.48490665]]
[[0. 0.30103 0.47712125 0.60205999]
[0.69897 0.77815125 0.84509804 0.90308999]
[0.95424251 1. 1.04139269 1.07918125]]
BY HARSH SHUKLA
0901IO211023
#matrix me harek element ko store karne me kitna bit laga hai for this we use itmesize
print(mx.itemsize)#the output is 4 but how ??
#so phele hum dtype ke through ye dhek lete hai ki total kitne bit le rha hai ye matrix
print(mx.dtype) #so the output is 32
#so me divide 32/8 and ans is 4
print("BY HARSH SHUKLA")
print("0901IO211023")
1
0
[ 1 2 3 4 5 6 7 8 9 10]
[ 1 11 21 31 41 51 61 71 81 91]
[[ 1]
[11]
[21]
[31]
[41]
[51]
[61]
[71]
[81]
[91]]
2
[[13 14 15]
[23 24 25]
[33 34 35]]
[[ 1 2]
[11 12]
[21 22]
[31 32]
[41 42]
[51 52]
[61 62]
[71 72]
[81 82]
[91 92]]
[[ 1 2 3 4 5 6 7 8 9 10]
[ 11 12 13 14 15 16 17 18 19 20]
[ 21 22 23 24 25 26 27 28 29 30]
[ 31 32 33 34 35 36 37 38 39 40]
[ 41 42 43 44 45 46 47 48 49 50]
[ 51 52 53 54 55 56 57 58 59 60]
[ 61 62 63 64 65 66 67 68 69 70]
[ 71 72 73 74 75 76 77 78 79 80]
[ 81 82 83 84 85 86 87 88 89 90]
[ 91 92 93 94 95 96 97 98 99 100]]
[[ 1 2 3 4 5 6 7 8 9 10]
[ 11 12 13 14 15 16 17 18 19 20]
[ 21 22 23 24 25 26 27 28 29 30]
[ 31 32 33 34 35 36 37 38 39 40]
[ 41 42 43 44 45 46 47 48 49 50]
[ 51 52 53 54 55 56 57 58 59 60]
[ 61 62 63 64 65 66 67 68 69 70]
[ 71 72 73 74 75 76 77 78 79 80]
[ 81 82 83 84 85 86 87 88 89 90]
[ 91 92 93 94 95 96 97 98 99 100]]
[[ 1 2 3 4 5 6 7 8 9 10]
[ 11 12 13 14 15 16 17 18 19 20]
[ 21 22 23 24 25 26 27 28 29 30]
[ 31 32 33 34 35 36 37 38 39 40]
[ 41 42 43 44 45 46 47 48 49 50]
[ 51 52 53 54 55 56 57 58 59 60]
[ 61 62 63 64 65 66 67 68 69 70]
[ 71 72 73 74 75 76 77 78 79 80]
[ 81 82 83 84 85 86 87 88 89 90]
[ 91 92 93 94 95 96 97 98 99 100]]
4
int32
BY HARSH SHUKLA
0901IO211023
#rand function
print(np.random.rand(3)) #so 1-d array create ho jayega or range (1,0) ke beech me koi bhee 3 numbers
print(np.random.rand(3,3)) #2-d array of size 3*3
print(np.random.rand(2,3,3)) #3-d array of size (3*3) * 2
#randn function
#ye humare random value deta hai (0,1) ke beech me but ye (+,-) values dono deta hai
print(np.random.randn(3)) #it returns the samples of standard normal distribution
#choice function
#ye hamare diye gaye sequence me se koi bhee ek value return kar dega
lst = [1,2,3,4]
print(np.random.choice(lst))
#clearing the concept of choice()
for i in range(20):
print(np.random.choice(lst))
#permutaion function
print(np.random.permutation(lst))# it returns the random permutation of given sequence
#clearing the concept of permutation()
for i in range(20):
print(np.random.permutation(lst))
print("HARSH SHUKLA")
print("0901IO211023")
[0.38656949]
[[0.15817412 0.60901665 0.87186768]
[0.3439397 0.05934477 0.6286641 ]
[0.76638949 0.32837078 0.66510341]]
4
[[3 2 2 3]
[1 3 2 4]
[4 3 1 3]
[2 1 4 2]]
[[[1 4 1 2]
[1 3 4 3]
[1 2 3 1]]
[[2 1 2 4]
[3 4 1 2]
[1 4 2 1]]
[[3 3 4 3]
[3 1 3 4]
[2 4 1 1]]]
[[2 2 1 4]
[1 2 4 1]
[2 2 1 2]
[2 3 1 2]]
[[2 2 1 4]
[1 2 4 1]
[2 2 1 2]
[2 3 1 2]]
[0.16911084 0.08833981 0.68535982]
[[0.95339335 0.00394827 0.51219226]
[0.81262096 0.61252607 0.72175532]
[0.29187607 0.91777412 0.71457578]]
[[[0.54254437 0.14217005 0.37334076]
[0.67413362 0.44183317 0.43401399]
[0.61776698 0.51313824 0.65039718]]
0 1
1 2
2 3
3 6
4 9
dtype: int64
<class 'pandas.core.series.Series'>
HARSH SHUKLA
0901IO211023
1 NaN
2 NaN
3 NaN
dtype: object
a 12
dtype: int64
HARSH SHUKLA
0901IO211023
0
0 1
1 3
2 4
3 5
4 6
<class 'pandas.core.frame.DataFrame'>
HARSH SHUKLA
0901IO211023
print("HARSH SHUKLA")
print("0901IO211023")
A B C
0 1 5 6
1 2 6 8
2 3 7 10
3 4 8 12
A B C D
0 1 5 True False
1 2 6 False False
2 3 7 True True
3 4 8 True True
A B C D
0 1 5 1 1.2
1 2 6 9 1.4
2 3 7 8 2.4
3 4 8 10 3.0
HARSH SHUKLA
0901IO211023
#Concat -> common part nhi dhekte hai bus do Series or data frames ko join kar deta hai
#first we concat two series
s1 = pd.Series([1,2,3,4])
s2 = pd.Series([5,6,7,8])
print(pd.concat([s1,s2]))#ek khas bat ki jab hum do Series ko add kar rhe honge toh humme unko list ke
through pass karna hai
#we can also concat two DataFrames
var11 = pd.DataFrame({"A":[1,2,3,4],"B":[3,4,5,6]})
var22 = pd.DataFrame({"A":[1,2,3,5],"C":[6,7,8,9]})
print(pd.concat([var11,var22],join = "outer")) #outer by default hota hai inner se sub NaN hut jayega
print(pd.concat([var11,var22],join = "inner",axis = 1)) #axis 1 dene se column wise a raha hai or axis 0 dene se
row wise ata
#agr mujhe NaN wala data hatana hai toh uske liye hum join kar istemaal karenge
#Append
#join me agr do column ke name same ho ja rhe the toh error a rha tha ya phir usko humko suffix ke through
solve karna pad rha tha
#but append me agr do column ka name bhee same hai toh koi dikkat nhi hai
a1= pd.DataFrame({"A":[1,2,3,4],"B":[3,4,5,6]})
a2 = pd.DataFrame({"A":[2,8],"D":[10,20]})
print(a1.append(a2,ignore_index = True))#ignore_index se hamari index sequence me ayegi........
print("HARSH SHUKLA")
print("0901IO211023")
OUTPUT OF 5TH MACRO PROGRAM - > 1.4:
A B C
0 1 3 6
1 2 4 7
2 3 5 8
A C B _merge
0 1 6 3 both
1 2 7 4 both
2 3 8 5 both
A C B _merge
0 1 6.0 3 both
1 2 7.0 4 both
2 3 8.0 5 both
3 4 NaN 6 right_only
AName BName Aid Bid
0 1 3 1 6
1 2 4 2 7
2 3 5 3 8
3 4 6 5 9
0 1
1 2
2 3
3 4
0 5
1 6
2 7
3 8
dtype: int64
A B C
0 1 3.0 NaN
1 2 4.0 NaN
2 3 5.0 NaN
3 4 6.0 NaN
0 1 NaN 6.0
1 2 NaN 7.0
2 3 NaN 8.0
3 5 NaN 9.0
A B A C
0 1 3 1 6
1 2 4 2 7
2 3 5 3 8
3 4 6 5 9
A B C D
0 1 3 2 10
1 2 4 8 20
2 3 5 9 30
3 4 6 5 50
A B C D
a 1 3 2.0 10.0
b 2 4 8.0 20.0
c 3 5 NaN NaN
d 4 6 NaN NaN
A B C D
a 1 3 2 10
b 2 4 8 20
A B A_21 D
a 1 3 2.0 10.0
b 2 4 8.0 20.0
c 3 5 NaN NaN
d 4 6 NaN NaN
C:\Users\hs081\PycharmProjects\Merging and Joining of DataFrames\main.py:59: FutureWarning: The
frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat
instead.
print(a1.append(a2,ignore_index = True))#ignore_index se hamari index sequence me ayegi........
A B D
0 1 3.0 NaN
1 2 4.0 NaN
2 3 5.0 NaN
3 4 6.0 NaN
4 2 NaN 10.0
5 8 NaN 20.0
HARSH SHUKLA
0901IO211023
HARSH SHUKLA
0901IO211023