MCPcopy
hub / github.com/pandas-dev/pandas / combine_first

Method combine_first

pandas/core/frame.py:10365–10439  ·  view source on GitHub ↗

Update null elements with value in the same location in `other`. Combine two DataFrame objects by filling null values in one DataFrame with non-null values from other DataFrame. The row and column indexes of the resulting DataFrame will be the union of the two. The

(self, other: DataFrame)

Source from the content-addressed store, hash-verified

10363 return frame_result.__finalize__(self, method="combine")
10364
10365 def combine_first(self, other: DataFrame) -> DataFrame:
10366 """
10367 Update null elements with value in the same location in `other`.
10368
10369 Combine two DataFrame objects by filling null values in one DataFrame
10370 with non-null values from other DataFrame. The row and column indexes
10371 of the resulting DataFrame will be the union of the two. The resulting
10372 dataframe contains the 'first' dataframe values and overrides the
10373 second one values where both first.loc[index, col] and
10374 second.loc[index, col] are not missing values, upon calling
10375 first.combine_first(second).
10376
10377 Parameters
10378 ----------
10379 other : DataFrame
10380 Provided DataFrame to use to fill null values.
10381
10382 Returns
10383 -------
10384 DataFrame
10385 The result of combining the provided DataFrame with the other object.
10386
10387 See Also
10388 --------
10389 DataFrame.combine : Perform series-wise operation on two DataFrames
10390 using a given function.
10391
10392 Examples
10393 --------
10394 >>> df1 = pd.DataFrame({"A": [None, 0], "B": [None, 4]})
10395 >>> df2 = pd.DataFrame({"A": [1, 1], "B": [3, 3]})
10396 >>> df1.combine_first(df2)
10397 A B
10398 0 1.0 3.0
10399 1 0.0 4.0
10400
10401 Null values still persist if the location of that null value
10402 does not exist in `other`
10403
10404 >>> df1 = pd.DataFrame({"A": [None, 0], "B": [4, None]})
10405 >>> df2 = pd.DataFrame({"B": [3, 3], "C": [1, 1]}, index=[1, 2])
10406 >>> df1.combine_first(df2)
10407 A B C
10408 0 NaN 4.0 NaN
10409 1 0.0 3.0 1.0
10410 2 NaN 3.0 1.0
10411 """
10412
10413 def combiner(x: Series, y: Series):
10414 # GH#60128 The combiner is supposed to preserve EA Dtypes.
10415 return y if y.name not in self.columns else y.where(x.isna(), x)
10416
10417 if len(other) == 0:
10418 combined = self.reindex(
10419 self.columns.append(other.columns.difference(self.columns)), axis=1
10420 )
10421 combined = combined.astype(other.dtypes)
10422 else:

Calls 8

reindexMethod · 0.95
combineMethod · 0.95
find_common_typeFunction · 0.90
intersectionMethod · 0.80
__finalize__Method · 0.80
appendMethod · 0.45
differenceMethod · 0.45
astypeMethod · 0.45