Skip to content

Added example for Dataframe.align #43225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8508,6 +8508,58 @@ def align(
-------
(left, right) : ({klass}, type of other)
Aligned objects.

Examples
--------
>>> left = pd.DataFrame(
... [[1, 2, 3, 4], [6, 7, 8, 9]], columns=["D", "B", "E", "A"], index=[1, 2]
... )

>>> right = pd.DataFrame(
... [[10, 20, 30, 40], [60, 70, 80, 90], [600, 700, 800, 900]],
... columns=["A", "B", "C", "D"],
... index=[2, 3, 4],
... )

>>> left
D B E A
1 1 2 3 4
2 6 7 8 9

>>> right
A B C D
2 10 20 30 40
3 60 70 80 90
4 600 700 800 900

>>> a1, a2 = left.align(right, join="outer", axis=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last thing - for consistency with the arguments, can we name the original dataframes df and other, and then the results left and right? So

Suggested change
>>> left = pd.DataFrame(
... [[1, 2, 3, 4], [6, 7, 8, 9]], columns=["D", "B", "E", "A"], index=[1, 2]
... )
>>> right = pd.DataFrame(
... [[10, 20, 30, 40], [60, 70, 80, 90], [600, 700, 800, 900]],
... columns=["A", "B", "C", "D"],
... index=[2, 3, 4],
... )
>>> left
D B E A
1 1 2 3 4
2 6 7 8 9
>>> right
A B C D
2 10 20 30 40
3 60 70 80 90
4 600 700 800 900
>>> a1, a2 = left.align(right, join="outer", axis=1)
>>> df = pd.DataFrame(
... [[1, 2, 3, 4], [6, 7, 8, 9]], columns=["D", "B", "E", "A"], index=[1, 2]
... )
>>> other = pd.DataFrame(
... [[10, 20, 30, 40], [60, 70, 80, 90], [600, 700, 800, 900]],
... columns=["A", "B", "C", "D"],
... index=[2, 3, 4],
... )
>>> df
D B E A
1 1 2 3 4
2 6 7 8 9
>>> other
A B C D
2 10 20 30 40
3 60 70 80 90
4 600 700 800 900
>>> left, right = left.align(right, join="outer", axis=1)

(likewise for the axis=0 example)


>>> a1
A B C D E
1 4 2 NaN 1 3
2 9 7 NaN 6 8

>>> a2
A B C D E
2 10 20 30 40 NaN
3 60 70 80 90 NaN
4 600 700 800 900 NaN

>>> b1, b2 = left.align(right, join="outer", axis=0)

>>> b1
D B E A
1 1.0 2.0 3.0 4.0
2 6.0 7.0 8.0 9.0
3 NaN NaN NaN NaN
4 NaN NaN NaN NaN

>>> b2
A B C D
1 NaN NaN NaN NaN
2 10.0 20.0 30.0 40.0
3 60.0 70.0 80.0 90.0
4 600.0 700.0 800.0 900.0
"""

method = missing.clean_fill_method(method)
Expand Down