Skip to content

Commit edc2e8d

Browse files
committed
ENH: Add sort_columns parameter to combine_first
1 parent 0b4ebc7 commit edc2e8d

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

pandas/core/frame.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8721,10 +8721,10 @@ def combine_first(
87218721
Combine two DataFrame objects by filling null values in one DataFrame
87228722
with non-null values from other DataFrame. The row and column indexes
87238723
of the resulting DataFrame will be the union of the two. The resulting
8724-
dataframe contains the 'first' dataframe values and overrides the
8725-
second one values where both first.loc[index, col] and
8726-
second.loc[index, col] are not missing values, upon calling
8727-
first.combine_first(second).
8724+
DataFrame contains the 'first' DataFrame values and overrides the
8725+
second one values where both `first.loc[index, col]` and
8726+
`second.loc[index, col]` are not missing values, upon calling
8727+
`first.combine_first(second)`.
87288728
87298729
Parameters
87308730
----------
@@ -8734,7 +8734,6 @@ def combine_first(
87348734
Whether to sort the columns in the result DataFrame. If False, the
87358735
order of the columns in `self` is preserved.
87368736
8737-
87388737
Returns
87398738
-------
87408739
DataFrame
@@ -8752,27 +8751,26 @@ def combine_first(
87528751
>>> df1 = pd.DataFrame({"A": [None, 0], "B": [None, 4]})
87538752
>>> df2 = pd.DataFrame({"A": [1, 1], "B": [3, 3]})
87548753
>>> df1.combine_first(df2)
8755-
A B
8754+
A B
87568755
0 1.0 3.0
87578756
1 0.0 4.0
87588757
8759-
87608758
Preserving the column order of `self` with `sort_columns=False`:
87618759
87628760
>>> df1 = pd.DataFrame({"B": [None, 4], "A": [0, None]})
87638761
>>> df2 = pd.DataFrame({"A": [1, 1], "B": [3, 3]})
87648762
>>> df1.combine_first(df2, sort_columns=False)
8765-
B A
8763+
B A
87668764
0 3.0 0.0
87678765
1 4.0 1.0
87688766
87698767
Null values still persist if the location of that null value
8770-
does not exist in `other`
8768+
does not exist in `other`.
87718769
87728770
>>> df1 = pd.DataFrame({"A": [None, 0], "B": [4, None]})
87738771
>>> df2 = pd.DataFrame({"B": [3, 3], "C": [1, 1]}, index=[1, 2])
87748772
>>> df1.combine_first(df2)
8775-
A B C
8773+
A B C
87768774
0 NaN 4.0 NaN
87778775
1 0.0 3.0 1.0
87788776
2 NaN 3.0 1.0

0 commit comments

Comments
 (0)