Python | Pandas dataframe.subtract()

22 Sep, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.subtract() function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs. 
Syntax: DataFrame.subtract(other, axis=’columns’, level=None, fill_value=None)Parameters : other : Series, DataFrame, or constant axis : For Series input, axis to match Series index on level : Broadcast across a level, matching Index values on the passed MultiIndex levelfill_value : Fill 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.Returns : result : DataFrame 
Example #1: Use subtract() function to subtract each element of a dataframe with a corresponding element in a series. 
Python3
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
default, selected
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
# importing pandas as pdimport pandas as pd # Creating the dataframedf = pd.DataFrame({"A":[1, 5, 3, 4, 2],                   "B":[3, 2, 4, 3, 4],                   "C":[2, 2, 7, 3, 4],                   "D":[4, 3, 6, 12, 7]},                   index =["A1", "A2", "A3", "A4", "A5"]) # Print the dataframedf
Let’s create the series 
Python3
# importing pandas as pdimport pandas as pd # Create the seriessr = pd.Series([12, 25, 64, 18], index =["A", "B", "C", "D"]) # Print the seriessr
Let’s use the dataframe.subtract() function for subtraction.
Python3
# equivalent to df - srdf.subtract(sr, axis = 1)
Output : 
  Example #2: Use subtract() function to subtract each element in a dataframe with the corresponding element in other dataframe
Python3
# importing pandas as pdimport pandas as pd # Creating the first dataframedf1 = pd.DataFrame({"A":[1, 5, 3, 4, 2],                    "B":[3, 2, 4, 3, 4],                    "C":[2, 2, 7, 3, 4],                    "D":[4, 3, 6, 12, 7]},                    index =["A1", "A2", "A3", "A4", "A5"]) # Creating the second dataframedf2 = pd.DataFrame({"A":[10, 11, 7, 8, 5],                    "B":[21, 5, 32, 4, 6],                    "C":[11, 21, 23, 7, 9],                    "D":[1, 5, 3, 8, 6]},                    index =["A1", "A2", "A3", "A4", "A5"]) # subtract df2 from df1df1.subtract(df2)
Output : 
Notice, each element of the dataframe df1 has been subtracted with the corresponding element in the df2. 
Python | Pandas dataframe.subtract() | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersPython | Pandas dataframe.subtract() | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:01•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=yHR-ZUmmrQQ" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
sweetyty
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe