- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.combine_first#
- DataFrame.combine_first(other)[source]#
Update null elements with value in the same location inother.
Combine two DataFrame objects by filling null values in one DataFramewith non-null values from other DataFrame. The row and column indexesof the resulting DataFrame will be the union of the two. The resultingdataframe contains the ‘first’ dataframe values and overrides thesecond one values where both first.loc[index, col] andsecond.loc[index, col] are not missing values, upon callingfirst.combine_first(second).
- Parameters:
- otherDataFrame
Provided DataFrame to use to fill null values.
- Returns:
- DataFrame
The result of combining the provided DataFrame with the other object.
See also
DataFrame.combine
Perform series-wise operation on two DataFrames using a given function.
Examples
>>>df1=pd.DataFrame({'A':[None,0],'B':[None,4]})>>>df2=pd.DataFrame({'A':[1,1],'B':[3,3]})>>>df1.combine_first(df2) A B0 1.0 3.01 0.0 4.0
Null values still persist if the location of that null valuedoes not exist inother
>>>df1=pd.DataFrame({'A':[None,0],'B':[4,None]})>>>df2=pd.DataFrame({'B':[3,3],'C':[1,1]},index=[1,2])>>>df1.combine_first(df2) A B C0 NaN 4.0 NaN1 0.0 3.0 1.02 NaN 3.0 1.0