Python pandas Series.compare() Method
In this tutorial, we will learn the python pandas Series.compare()
method. This method compares two Series and shows the differences. When this method applied to the Series it returns the Series
if the axis is 0 or ‘index’ the result will be a Series or the DataFrame if the axis is 1 or ‘columns’ the result will be a DataFrame.
The below shows the syntax of the Series.compare()
method.
Syntax
Series.compare(other, align_axis=1, keep_shape=False, keep_equal=False)
other: It is the Series that is the object to compare with.
align_axis: If it is '0' means ‘index’ and if it is '1' means ‘columns’, and the default value is 1. It determines which axis to align the comparison on.
keep_shape: It represents the bool(True or False), and the default value is False. If it is True, all rows and columns are kept. Otherwise, only the ones with different values are kept.
keep_equal: It represents the bool(True or False), and the default value is False. If it is true, the result keeps values that are equal. Otherwise, equal values are shown as NaNs.
Example: Compare two Series using the Series.compare()
method
Here, in this example, we are comparing two Series using the Series.compare()
method. The Series.compare()
method returns a DataFrame which shows the differences with the 'self ' and 'other' columns. See the below example.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,np.nan,2,4,5])
series_2 = pd.Series([4,5,2,np.nan,7])
print(series_1.compare(series_2))
self other
0 1.0 4.0
1 NaN 5.0
3 4.0 NaN
4 5.0 7.0
Example: Compare two Series using the Series.compare()
method
Here, in this example, we are comparing two Series using the Series.compare()
method along the index axis. The Series.compare()
method returns a Series and the resulting index will be a MultiIndex with ‘self’ and ‘other’ stacked alternately at the inner level. See the below example.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,np.nan,2,4,5])
series_2 = pd.Series([4,5,2,np.nan,7])
print(series_1.compare(series_2, align_axis=0))
0 self 1.0
other 4.0
1 self NaN
other 5.0
3 self 4.0
other NaN
4 self 5.0
other 7.0
dtype: float64
Example: Compare two Series using the Series.compare()
method keep_shape=True
If keep_shape=True
, all rows and columns in the resulted DataFrame will be shown. Otherwise, only the ones with different values will be shown in the resulted DataFrame. See the below example.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,np.nan,2,4,5])
series_2 = pd.Series([4,5,2,np.nan,7])
print(series_1.compare(series_2, keep_shape=True))
self other
0 1.0 4.0
1 NaN 5.0
2 NaN NaN
3 4.0 NaN
4 5.0 7.0
Example: Compare two Series using the Series.compare()
method with keep_shape=True
If keep_shape=True
, the result keeps values that are equal. Otherwise, equal values are shown as NaNs. See the below example.
#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
series_1 = pd.Series([1,np.nan,2,4,5])
series_2 = pd.Series([4,5,2,np.nan,7])
print(series_1.compare(series_2, keep_shape=True, keep_equal=True))
self other
0 1.0 4.0
1 NaN 5.0
2 2.0 2.0
3 4.0 NaN
4 5.0 7.0
Conclusion
In this tutorial, we learned the Python pandas Series.compare()
method. We learned the syntax and parameters of the Series.compare()
method. We compared two dataframes by examples and understood the Series.compare()
method.