@@ -8712,7 +8712,7 @@ def combine(
8712
8712
frame_result = self ._constructor (result , index = new_index , columns = new_columns )
8713
8713
return frame_result .__finalize__ (self , method = "combine" )
8714
8714
8715
- def combine_first (self , other : DataFrame ) -> DataFrame :
8715
+ def combine_first (self , other : DataFrame , sort_columns = True ) -> DataFrame :
8716
8716
"""
8717
8717
Update null elements with value in the same location in `other`.
8718
8718
@@ -8728,6 +8728,10 @@ def combine_first(self, other: DataFrame) -> DataFrame:
8728
8728
----------
8729
8729
other : DataFrame
8730
8730
Provided DataFrame to use to fill null values.
8731
+ sort_columns : bool, default True
8732
+ Whether to sort the columns in the result DataFrame. If False, the
8733
+ order of the columns in `self` is preserved.
8734
+
8731
8735
8732
8736
Returns
8733
8737
-------
@@ -8741,13 +8745,25 @@ def combine_first(self, other: DataFrame) -> DataFrame:
8741
8745
8742
8746
Examples
8743
8747
--------
8748
+ Default behavior with `sort_columns=True` (default):
8749
+
8744
8750
>>> df1 = pd.DataFrame({"A": [None, 0], "B": [None, 4]})
8745
8751
>>> df2 = pd.DataFrame({"A": [1, 1], "B": [3, 3]})
8746
8752
>>> df1.combine_first(df2)
8747
8753
A B
8748
8754
0 1.0 3.0
8749
8755
1 0.0 4.0
8750
8756
8757
+
8758
+ Preserving the column order of `self` with `sort_columns=False`:
8759
+
8760
+ >>> df1 = pd.DataFrame({"B": [None, 4], "A": [0, None]})
8761
+ >>> df2 = pd.DataFrame({"A": [1, 1], "B": [3, 3]})
8762
+ >>> df1.combine_first(df2, sort_columns=False)
8763
+ B A
8764
+ 0 3.0 0.0
8765
+ 1 4.0 1.0
8766
+
8751
8767
Null values still persist if the location of that null value
8752
8768
does not exist in `other`
8753
8769
@@ -8773,6 +8789,8 @@ def combiner(x: Series, y: Series):
8773
8789
return y_values
8774
8790
8775
8791
return expressions .where (mask , y_values , x_values )
8792
+
8793
+ all_columns = self .columns .union (other .columns )
8776
8794
8777
8795
if len (other ) == 0 :
8778
8796
combined = self .reindex (
@@ -8790,6 +8808,13 @@ def combiner(x: Series, y: Series):
8790
8808
8791
8809
if dtypes :
8792
8810
combined = combined .astype (dtypes )
8811
+
8812
+ combined = combined .reindex (columns = all_columns , fill_value = None )
8813
+
8814
+ if not sort_columns :
8815
+ combined = combined [self .columns ]
8816
+
8817
+
8793
8818
8794
8819
return combined .__finalize__ (self , method = "combine_first" )
8795
8820
0 commit comments