### Unit 2 - Binary operations in a Data Frame

CBSE Revision Notes

Class-11 Computer Science (New Syllabus)
Unit 2: Data Handling (DH-1)

Binary operations in a Data Frame

`DataFrame.``add`(otheraxis='columns'level=Nonefill_value=None)

Equivalent to `dataframe + other`, but with support to substitute a fill_value for missing data in one of the inputs.

Parameters: other : Series, DataFrame, or constantaxis : {0, 1, ‘index’, ‘columns’}For Series input, axis to match Series index onlevel : int or nameBroadcast across a level, matching Index values on the passed MultiIndex levelfill_value : None or float value, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing result : DataFrame

Notes : Mismatched indices will be unioned together

Examples

`>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],...                  columns=['one'])>>> a   onea  1.0b  1.0c  1.0d  NaN>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],...                       two=[np.nan, 2, np.nan, 2]),...                  index=['a', 'b', 'd', 'e'])>>> b   one  twoa  1.0  NaNb  NaN  2.0d  1.0  NaNe  NaN  2.0>>> a.add(b, fill_value=0)   one  twoa  2.0  NaNb  1.0  2.0c  1.0  NaNd  1.0  NaNe  NaN  2.0`

pandas.DataFrame.sub

`DataFrame.``sub`(otheraxis='columns'level=Nonefill_value=None)

Subtraction of dataframe and other, element-wise (binary operator sub).

Equivalent to `dataframe - other`, but with support to substitute a fill_value for missing data in one of the inputs.

Parameters: other : Series, DataFrame, or constantaxis : {0, 1, ‘index’, ‘columns’}For Series input, axis to match Series index onlevel : int or nameBroadcast across a level, matching Index values on the passed MultiIndex levelfill_value : None or float value, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing result : DataFrame

Notes : Mismatched indices will be unioned together

Examples

`>>> a = pd.DataFrame([2, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],...                  columns=['one'])>>> a   onea  2.0b  1.0c  1.0d  NaN>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],...                       two=[3, 2, np.nan, 2]),...                  index=['a', 'b', 'd', 'e'])>>> b   one  twoa  1.0  3.0b  NaN  2.0d  1.0  NaNe  NaN  2.0>>> a.sub(b, fill_value=0)   one  twoa  1.0  -3.0b  1.0  -2.0c  1.0  NaNd  -1.0  NaNe  NaN  -2.0`

pandas.DataFrame.mul

`DataFrame.``mul`(otheraxis='columns'level=Nonefill_value=None)

Multiplication of dataframe and other, element-wise (binary operator mul).

Equivalent to `dataframe * other`, but with support to substitute a fill_value for missing data in one of the inputs.

Parameters: other : Series, DataFrame, or constantaxis : {0, 1, ‘index’, ‘columns’}For Series input, axis to match Series index onlevel : int or nameBroadcast across a level, matching Index values on the passed MultiIndex levelfill_value : None or float value, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing result : DataFrame

Notes : Mismatched indices will be unioned together

pandas.DataFrame.div

`DataFrame.``div`(otheraxis='columns'level=Nonefill_value=None)

Floating division of dataframe and other, element-wise (binary operator truediv).

Equivalent to `dataframe / other`, but with support to substitute a fill_value for missing data in one of the inputs.

Parameters: other : Series, DataFrame, or constantaxis : {0, 1, ‘index’, ‘columns’}For Series input, axis to match Series index onlevel : int or nameBroadcast across a level, matching Index values on the passed MultiIndex levelfill_value : None or float value, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing result : DataFrame

Notes : Mismatched indices will be unioned together

`DataFrame.``radd`(otheraxis='columns'level=Nonefill_value=None)

Equivalent to `other + dataframe`, but with support to substitute a fill_value for missing data in one of the inputs.

Parameters: other : Series, DataFrame, or constantaxis : {0, 1, ‘index’, ‘columns’}For Series input, axis to match Series index onlevel : int or nameBroadcast across a level, matching Index values on the passed MultiIndex levelfill_value : None or float value, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing result : DataFrame

Notes : Mismatched indices will be unioned together

Examples

`>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],...                  columns=['one'])>>> a   onea  1.0b  1.0c  1.0d  NaN>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],...                       two=[np.nan, 2, np.nan, 2]),...                  index=['a', 'b', 'd', 'e'])>>> b   one  twoa  1.0  NaNb  NaN  2.0d  1.0  NaNe  NaN  2.0>>> a.add(b, fill_value=0)   one  twoa  2.0  NaNb  1.0  2.0c  1.0  NaNd  1.0  NaNe  NaN  2.0`

pandas.DataFrame.rsub

`DataFrame.``rsub`(otheraxis='columns'level=Nonefill_value=None)

Subtraction of dataframe and other, element-wise (binary operator rsub).

Equivalent to `other - dataframe`, but with support to substitute a fill_value for missing data in one of the inputs.

Parameters: other : Series, DataFrame, or constantaxis : {0, 1, ‘index’, ‘columns’}For Series input, axis to match Series index onlevel : int or nameBroadcast across a level, matching Index values on the passed MultiIndex levelfill_value : None or float value, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing result : DataFrame

Notes : Mismatched indices will be unioned together

Examples

`>>> a = pd.DataFrame([2, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],...                  columns=['one'])>>> a   onea  2.0b  1.0c  1.0d  NaN>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],...                       two=[3, 2, np.nan, 2]),...                  index=['a', 'b', 'd', 'e'])>>> b   one  twoa  1.0  3.0b  NaN  2.0d  1.0  NaNe  NaN  2.0>>> a.sub(b, fill_value=0)   one  twoa  1.0  -3.0b  1.0  -2.0c  1.0  NaNd  -1.0  NaNe  NaN  -2.0`