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.
combine_first() method is used to combine two series into one. The result is union of the two series that is in case of Null value in caller series, the value from passed series is taken. In case of both null values at the same index, null is returned at that index.
Note: This method is different from Series.combine() which takes a function as parameter to decide output value.
other: Other series to be combined with caller series.
Return type: Pandas series
In this example, two series are created from list using Pandas
Series() method. Some Null values are also passed to each list using Numpy
np.nan. Both series are then combined using
.combine_first() method. At first, the method is called by series1 and result is stored in result1 and then similarly it is called by series2 and stored in result2. Both of the returned series are then printed to compare outputs.
As shown in the output, even though the same series were combined, but the outputs are different. This is because of
combine_first() method prioritize first series ( Caller series ) before. If there is null value at that position, it takes value at same index from second series.
Result 1: 0 70.0 1 5.0 2 0.0 3 225.0 4 1.0 5 16.0 6 53.0 7 10.0 8 5.0 dtype: float64 Result 2: 0 27.0 1 5.0 2 2.0 3 23.0 4 1.0 5 95.0 6 53.0 7 10.0 8 5.0 dtype: float64
- Python | pandas.map()
- Python | Pandas Series.sem()
- Python | Pandas TimedeltaIndex.min
- Python | Pandas dataframe.sub()
- Python | Pandas Series.between()
- Python | Pandas DataFrame.where()
- Python | Pandas DataFrame.loc
- Python | Pandas series.str.get()
- Python | Pandas TimedeltaIndex.max
- Python | Pandas Series.std()
- Python | Pandas DataFrame.ix[ ]
- Python | Pandas DataFrame.abs()
- Python | Pandas dataframe.get()
- Python | Pandas Series.take()
- Python | Pandas Timestamp.now
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.