Skip to content

Commit f6ae271

Browse files
oovkMarcoGorelli
andauthored
Added example for Dataframe.align (#43225)
* added example for Dataframe.align * added example for axis=0 param * changes in align example * passed linting checks * made requested changes * fixup Co-authored-by: Marco Gorelli <[email protected]>
1 parent 2b3a9b1 commit f6ae271

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

pandas/core/generic.py

+65
Original file line numberDiff line numberDiff line change
@@ -8508,6 +8508,71 @@ def align(
85088508
-------
85098509
(left, right) : ({klass}, type of other)
85108510
Aligned objects.
8511+
8512+
Examples
8513+
--------
8514+
>>> df = pd.DataFrame(
8515+
... [[1, 2, 3, 4], [6, 7, 8, 9]], columns=["D", "B", "E", "A"], index=[1, 2]
8516+
... )
8517+
>>> other = pd.DataFrame(
8518+
... [[10, 20, 30, 40], [60, 70, 80, 90], [600, 700, 800, 900]],
8519+
... columns=["A", "B", "C", "D"],
8520+
... index=[2, 3, 4],
8521+
... )
8522+
>>> df
8523+
D B E A
8524+
1 1 2 3 4
8525+
2 6 7 8 9
8526+
>>> other
8527+
A B C D
8528+
2 10 20 30 40
8529+
3 60 70 80 90
8530+
4 600 700 800 900
8531+
8532+
Align on columns:
8533+
8534+
>>> left, right = df.align(other, join="outer", axis=1)
8535+
>>> left
8536+
A B C D E
8537+
1 4 2 NaN 1 3
8538+
2 9 7 NaN 6 8
8539+
>>> right
8540+
A B C D E
8541+
2 10 20 30 40 NaN
8542+
3 60 70 80 90 NaN
8543+
4 600 700 800 900 NaN
8544+
8545+
We can also align on the index:
8546+
8547+
>>> left, right = df.align(other, join="outer", axis=0)
8548+
>>> left
8549+
D B E A
8550+
1 1.0 2.0 3.0 4.0
8551+
2 6.0 7.0 8.0 9.0
8552+
3 NaN NaN NaN NaN
8553+
4 NaN NaN NaN NaN
8554+
>>> right
8555+
A B C D
8556+
1 NaN NaN NaN NaN
8557+
2 10.0 20.0 30.0 40.0
8558+
3 60.0 70.0 80.0 90.0
8559+
4 600.0 700.0 800.0 900.0
8560+
8561+
Finally, the default `axis=None` will align on both index and columns:
8562+
8563+
>>> left, right = df.align(other, join="outer", axis=None)
8564+
>>> left
8565+
A B C D E
8566+
1 4.0 2.0 NaN 1.0 3.0
8567+
2 9.0 7.0 NaN 6.0 8.0
8568+
3 NaN NaN NaN NaN NaN
8569+
4 NaN NaN NaN NaN NaN
8570+
>>> right
8571+
A B C D E
8572+
1 NaN NaN NaN NaN NaN
8573+
2 10.0 20.0 30.0 40.0 NaN
8574+
3 60.0 70.0 80.0 90.0 NaN
8575+
4 600.0 700.0 800.0 900.0 NaN
85118576
"""
85128577

85138578
method = missing.clean_fill_method(method)

0 commit comments

Comments
 (0)