Pandas DataFrame | sub method
Start your free 7-days trial now!
Pandas DataFrame.sub(~) method subtracts a scalar, sequence, Series or DataFrame from the values in the source DataFrame, that is:
DataFrame - other
Unless you use the parameters axis, level and fill_value, sub(~) is equivalent to performing subtraction using the - operator.
Parameters
1. otherlink | scalar or sequence or Series or DataFrame
The resulting DataFrame will be other subtracted from the source DataFrame.
2. axislink | int or string | optional
Whether to broadcast other for each column or row of the source DataFrame:
Axis | Description |
|---|---|
|
|
|
|
This is only relevant if the dimensions of the source DataFrame and other do not align. By default, axis="columns".
3. level | int or string | optional
The name or the integer index of the level to consider. This is relevant if your DataFrame is Multi-index.
4. fill_valuelink | float or None | optional
The value to replace NaN before the computation. If the subtraction involves two NaN, then the result would still be NaN. By default, fill_value=None.
Return Value
A new DataFrame resulting from the subtraction of other from the source DataFrame.
Examples
Basic usage
Consider the following DataFrames:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})df_other = pd.DataFrame({"A":[9,8], "B":[7,6]})
A B | A B0 2 4 | 0 9 71 3 5 | 1 8 6
Subtracting df_other from df yields:
df.sub(df_other)
A B0 -7 -31 -5 -1
Broadcasting
Consider the following DataFrame:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})df
A B0 2 41 3 5
Row-wise subtraction
By default, axis=1, which means that other will be broadcasted for each row in df:
df.sub([6,7]) # axis=1
A B0 -4 -31 -3 -2
Here, we're doing the following element-wise subtraction:
2-6 4-73-6 5-7
Column-wise subtraction
To broadcast other for each column in df, set axis=0 like so:
df.sub([6,7], axis=0)
A B0 -4 -21 -4 -2
Here, we're doing the following element-wise subtraction:
2-6 4-63-7 5-7
Specifying fill_value
Consider the following DataFrames:
df = pd.DataFrame({"A":[2,np.NaN], "B":[np.NaN,5]})df_other = pd.DataFrame({"A":[10,20],"B":[np.NaN,np.NaN]})
A B | A B0 2.0 NaN | 0 10 NaN1 NaN 5.0 | 1 20 NaN
By default, when we perform subtraction using the sub(~) method, any operation with NaN results in NaN:
df.sub(df_other)
A B0 -8.0 NaN1 NaN NaN
We can fill the NaN values before we perform subtraction by using the fill_value parameter like so:
df.sub(df_other, fill_value=100)
A B0 -8.0 NaN1 80.0 -95.0
Here, notice how the result of the subtraction between two NaN is still NaN, regardless of fill_value.