Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

pdpark
Copy link

@pdpark pdpark commented Jan 14, 2018

@gfyoung gfyoung added Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jan 24, 2018
# 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]])
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the names parameter here, instead of setting it below.

ser.index.names=['Let','Num']
ser

# reset_index turns the multi-level row index into columns, which requires a DataFrame
Copy link
Contributor

Choose a reason for hiding this comment

The 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'])

@@ -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:
Copy link
Contributor

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

To join a Series with a MultiIndex and a DataFrame, using the levels of the
MultiIndex and columns from the DataFrame, transform the Series to a DataFrame
using :meth:`Series.reset_index`.

type(df2)

# Now we merge the DataFrames
pd.merge(df, df2, on=['Let','Num'])
Copy link
Contributor

Choose a reason for hiding this comment

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

Space after the comma.

Copy link
Author

Choose a reason for hiding this comment

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

will do

ser

# Name the row index levels
ser.index.names=['Let','Num']
Copy link
Contributor

Choose a reason for hiding this comment

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

Space after the comma.

# 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]])
Copy link
Contributor

Choose a reason for hiding this comment

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

Spaces around the *

@codecov
Copy link

codecov bot commented Jan 30, 2018

Codecov Report

Merging #19235 into master will decrease coverage by 0.27%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #19235      +/-   ##
==========================================
- Coverage   91.82%   91.55%   -0.28%     
==========================================
  Files         152      147       -5     
  Lines       49248    48797     -451     
==========================================
- Hits        45222    44675     -547     
- Misses       4026     4122      +96
Flag Coverage Δ
#multiple 89.92% <ø> (-0.29%) ⬇️
#single 41.6% <ø> (-0.29%) ⬇️
Impacted Files Coverage Δ
pandas/tseries/plotting.py 0% <0%> (-100%) ⬇️
pandas/plotting/_compat.py 62% <0%> (-28.91%) ⬇️
pandas/core/missing.py 84.3% <0%> (-7.35%) ⬇️
pandas/core/accessor.py 93.75% <0%> (-4.96%) ⬇️
pandas/plotting/_timeseries.py 60.82% <0%> (-4.49%) ⬇️
pandas/core/ops.py 92.06% <0%> (-4.29%) ⬇️
pandas/core/categorical.py 95.78% <0%> (-4.22%) ⬇️
pandas/core/reshape/tile.py 90.25% <0%> (-3.12%) ⬇️
pandas/io/html.py 85.98% <0%> (-2.81%) ⬇️
pandas/io/formats/format.py 96.24% <0%> (-2.01%) ⬇️
... and 78 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cdfce2b...31bb36a. Read the comment docs.

@jreback
Copy link
Contributor

jreback commented Aug 2, 2018

can you rebase and update

@pdpark
Copy link
Author

pdpark commented Aug 31, 2018

Will do - have been absent due to starting new job, but plan to spend some time on this.

@datapythonista
Copy link
Member

Closing as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Equivalent of Series.map for DataFrame
5 participants