So far, we have learned many concepts in Pandas,
Now we will learn about various operations that we can perform in regards to dataFrames,

All these categories mentioned above perform various operations that could be helpful in different kinds of data analysis. So, let’s study them in depth now,
Binary Operations
Binary means ‘two’, if we perform any operation between two elements then it is a Binary Operation. This includes addition, subtraction, multiplication and division. Since we are considering dataFrames here, so their operations are in regard to two dataFrames, like adding, subtracting or multiplying elements of two dataFrames.
+ , add(), radd()
- If 2 dataFrames are all numeric and we want to add those 2 dataFrames, then we use ‘+’.
SYNTAX
dataFrame1-dataFrame2
- For the addition of 2 dataFrames we can also use the method ‘add()’.
SYNTAX
dataFrame1.add(dataFrame2)
- Also, you can use ‘radd()’, this works the same as add(), the difference is that if we want A+B, we use add(), else if we want B+A, we use radd(). (It won’t make any difference in addition but it would make sense when we need subtraction and division.)
SYNTAX
dataFrame1.radd(dataFrame2)
- import pandas as pd
- dict1= {'A':[85,73,98], 'B':[60,80,58], 'C':[90,60,74], 'D': [95,87,92] }
- df1=pd.DataFrame(dict1,index=['0','1','2'])
- print("This is df1:")
- print(df1)
- print('\n')
- dict2= {'A':[85,73,98], 'B':[60,80,58], 'C':[90,60,74], D': [95,87,92] }
- df2=pd.DataFrame(dict2,index=['0','1','2'])
- print("This is df2:")
- print(df2)
- print('\n')
- df3=df1+df2
- print("Using '+', This is df1+df2 :")
- print("This is df3:")
- print(df3)
- print('\n')
- df4=df2.add(df3)
- print("Using 'add()', This is df2+df3 :")
- print("This is df4:")
- print(df4)
- print('\n')
- df5=df3.radd(df4)
- print("Using 'radd()', This is df4+df3:")
- print(df5)
OUTPUT

- , sub(),rsub()
- If you want to perform subtraction within 2 dataFrames then you can use ‘-‘ or method ‘sub()’.
SYNTAX
dataFrame1-dataFrame2
SYNTAX
dataFrame1.sub(dataFrame2)
- As mentioned above, if you want A-B, then use ‘sub()’, but if you want B-A, then use ‘rsub()’
SYNTAX
dataFrame1.rsub(dataFrame2)
- For B-A, you can also use,
SYNTAX
dataFrame2-dataFrame1
- import pandas as pd
- dict1= {'A':[85,73,98], 'B':[60,80,58],'C':[90,60,74], 'D': [95,87,92] }
- df1=pd.DataFrame(dict1,index=['0','1','2'])
- print("This is df1:")
- print(df1)
- print('\n')
- dict2= {'A':[8,7,9], 'B':[6,8,5], 'C':[9,6,7], 'D': [5,8,2] }
- df2=pd.DataFrame(dict2,index=['0','1','2'])
- print("This is df2:")
- print(df2)
- print('\n')
- df3=df1.sub(df2)
- print("Using 'sub()', This is df1-df2 :")
- print(df3)
- print('\n')
- df4=df1.rsub(df2)
- print("Using 'rsub()', This is df2-df1 :")
- print(df4)
OUTPUT

* , mul(), rmul()
If you want to multiply 2 dataFrames then you can use ‘*‘ or method ‘mul()’.
SYNTAX
dataFrame1*dataFrame2
SYNTAX
dataFrame1.mul(dataFrame2)
‘rmul()’ works same as radd()
SYNTAX
dataFrame1.rmul(dataFrame2)
- import pandas as pd
- dict1= {'A':[85,73,98], 'B':[60,80,58], 'C':[90,60,74], D': [95,87,92] }
- df1=pd.DataFrame(dict1,index=['0','1','2'])
- print("This is df1:")
- print(df1)
- print('\n')
- dict2= {'A':[8,7,9], 'B':[6,8,5], 'C':[9,6,7], 'D': [5,8,2] }
- df2=pd.DataFrame(dict2,index=['0','1','2'])
- print("This is df2:")
- print(df2)
- print('\n')
- df3=df1*df2
- print("Using '*', This is df1*df2 :")
- print("This is df3:")
- print(df3)
- print('\n')
- df4=df2.mul(df3)
- print("Using 'mul()', This is df2*df3 :")
- print(df4)
OUTPUT

/ , div(), rdiv()
- If you want to perform division within 2 dataFrames then you can use ‘/‘ or method ‘div()’.
SYNTAX
dataFrame1/dataFrame2
SYNTAX
dataFrame1.div(dataFrame2)
- As mentioned above, if you want A/B, then use ‘div()’, but if you want B-A, then use ‘rdiv()’
SYNTAX
dataFrame1.rdiv(dataFrame2)







Join the conversation! Your thoughts help the community grow.