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.
dataframe.sub() 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.sub(other, axis=’columns’, level=None, fill_value=None)
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 leve
fill_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
sub() function to subtract each element of a dataframe with a corresponding element in a series.
Let’s create the series
Let’s use the
dataframe.sub() function for subtraction.
Example #2: Use
sub() function to subtract each element in a dataframe with the corresponding element in other dataframe
Notice, each element of the dataframe df1 has been subtracted with the corresponding element in the df2.
- Python | pandas.map()
- Python | Pandas Series.iat
- Python | Pandas Series.eq()
- Python | Pandas Series.loc
- Python | Pandas Series.ix
- Python | Pandas.apply()
- Python | Pandas Series.var
- Python | Pandas Series.where
- Python | Pandas Series.xs
- Python | Pandas Series.ne()
- Python | Pandas Series.str.len()
- Python | Pandas Series.lt()
- Python | Pandas Timestamp.day
- Python | Pandas PeriodIndex.second
- Python | Pandas Series.str.contains()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.