-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Doc: Adds example of joining a series to a dataframe. #19235
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -714,6 +714,31 @@ either the left or right tables, the values in the joined table will be | |
labels=['left', 'right'], vertical=False); | ||
plt.close('all'); | ||
|
||
To join a Series and a DataFrame, the Series has to be transformed into a DataFrame first: | ||
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) | ||
df | ||
|
||
# The series has a multi-index with levels corresponding to columns in the DataFrame we want to merge with | ||
ser = pd.Series( | ||
['a', 'b', 'c', 'd', 'e', 'f'], | ||
index=pd.MultiIndex.from_arrays([["A", "B", "C"]*2, [1, 2, 3, 4, 5, 6]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around the |
||
) | ||
ser | ||
|
||
# Name the row index levels | ||
ser.index.names=['Let','Num'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space after the comma. |
||
ser | ||
|
||
# reset_index turns the multi-level row index into columns, which requires a DataFrame | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be removed, to make the example shorter. I'd just write it as # Convert the Series to a DataFrame and merge
pd.merge(df, ser.reset_index(), on=['Let', 'Num']) |
||
df2 = ser.reset_index() | ||
type(df2) | ||
|
||
# Now we merge the DataFrames | ||
pd.merge(df, df2, on=['Let','Num']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space after the comma. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do |
||
|
||
Here is another example with duplicate join keys in ``DataFrame``s: | ||
|
||
.. ipython:: python | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explain that the
Series
is a MultiIndexed series, and that's what you're joining on. Something like