Skip to content

Commit d579ab6

Browse files
author
Patrick D. Park
committed
Doc: Adds example of joining a series to a dataframe.
Resolves: pandas-dev#12550
1 parent 7ed1f53 commit d579ab6

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

doc/source/merging.rst

+30-12
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ functionality below.
152152
Set logic on the other axes
153153
~~~~~~~~~~~~~~~~~~~~~~~~~~~
154154

155-
When gluing together multiple DataFrames, you have a choice of how to handle
155+
When gluing together multiple ``DataFrame``s, you have a choice of how to handle
156156
the other axes (other than the one being concatenated). This can be done in
157157
the following three ways:
158158
@@ -323,13 +323,6 @@ the name of the ``Series``.
323323
labels=['df1', 's1'], vertical=False);
324324
plt.close('all');
325325
326-
.. note::
327-
328-
Since we're concatenating a ``Series`` to a ``DataFrame``, we could have
329-
achieved the same result with :meth:`DataFrame.assign`. To concatenate an
330-
arbitrary number of pandas objects (``DataFrame`` or ``Series``), use
331-
``concat``.
332-
333326
If unnamed ``Series`` are passed they will be numbered consecutively.
334327

335328
.. ipython:: python
@@ -583,7 +576,7 @@ and ``right`` is a subclass of DataFrame, the return type will still be
583576

584577
``merge`` is a function in the pandas namespace, and it is also available as a
585578
``DataFrame`` instance method :meth:`~DataFrame.merge`, with the calling
586-
``DataFrame`` being implicitly considered the left object in the join.
579+
``DataFrame `` being implicitly considered the left object in the join.
587580
588581
The related :meth:`~DataFrame.join` method, uses ``merge`` internally for the
589582
index-on-index (by default) and column(s)-on-index join. If you are joining on
@@ -636,7 +629,7 @@ key combination:
636629
637630
Here is a more complicated example with multiple join keys. Only the keys
638631
appearing in ``left`` and ``right`` are present (the intersection), since
639-
``how='inner'`` by default.
632+
``how='inner'```by default.
640633
641634
.. ipython:: python
642635
@@ -721,7 +714,32 @@ either the left or right tables, the values in the joined table will be
721714
labels=['left', 'right'], vertical=False);
722715
plt.close('all');
723716
724-
Here is another example with duplicate join keys in DataFrames:
717+
To join a Series and a DataFrame, the Series has to be transformed into a DataFrame first:
718+
719+
.. ipython:: python
720+
721+
df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]})
722+
df
723+
724+
# The series has a multi-index with levels corresponding to columns in the DataFrame we want to merge with
725+
ser = pd.Series(
726+
['a', 'b', 'c', 'd', 'e', 'f'],
727+
index=pd.MultiIndex.from_arrays([["A", "B", "C"]*2, [1, 2, 3, 4, 5, 6]])
728+
)
729+
ser
730+
731+
# Name the row index levels
732+
ser.index.names=['Let','Num']
733+
ser
734+
735+
# reset_index turns the multi-level row index into columns, which requires a DataFrame
736+
df2 = ser.reset_index()
737+
type(df2)
738+
739+
# Now we merge the DataFrames
740+
pd.merge(df, df2, on=['Let','Num'])
741+
742+
Here is another example with duplicate join keys in ``DataFrame``s:
725743
726744
.. ipython:: python
727745
@@ -1202,7 +1220,7 @@ Overlapping value columns
12021220
~~~~~~~~~~~~~~~~~~~~~~~~~
12031221

12041222
The merge ``suffixes`` argument takes a tuple of list of strings to append to
1205-
overlapping column names in the input ``DataFrame``\ s to disambiguate the result
1223+
overlapping column names in the input ``DataFrame``s to disambiguate the result
12061224
columns:
12071225
12081226
.. ipython:: python

0 commit comments

Comments
 (0)