Skip to content

Commit 7c0b4a3

Browse files
palewirejorisvandenbossche
authored andcommitted
DOC: Added examples to pandas.concat documentation (pandas-dev#15028)
1 parent 098831d commit 7c0b4a3

File tree

1 file changed

+137
-7
lines changed

1 file changed

+137
-7
lines changed

pandas/tools/merge.py

+137-7
Original file line numberDiff line numberDiff line change
@@ -1398,9 +1398,11 @@ def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
13981398
copy=True):
13991399
"""
14001400
Concatenate pandas objects along a particular axis with optional set logic
1401-
along the other axes. Can also add a layer of hierarchical indexing on the
1402-
concatenation axis, which may be useful if the labels are the same (or
1403-
overlapping) on the passed axis number
1401+
along the other axes.
1402+
1403+
Can also add a layer of hierarchical indexing on the concatenation axis,
1404+
which may be useful if the labels are the same (or overlapping) on
1405+
the passed axis number.
14041406
14051407
Parameters
14061408
----------
@@ -1436,13 +1438,141 @@ def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
14361438
copy : boolean, default True
14371439
If False, do not copy data unnecessarily
14381440
1439-
Notes
1440-
-----
1441-
The keys, levels, and names arguments are all optional
1442-
14431441
Returns
14441442
-------
14451443
concatenated : type of objects
1444+
1445+
Notes
1446+
-----
1447+
The keys, levels, and names arguments are all optional.
1448+
1449+
A walkthrough of how this method fits in with other tools for combining
1450+
panda objects can be found `here
1451+
<http://pandas.pydata.org/pandas-docs/stable/merging.html>`__.
1452+
1453+
See Also
1454+
--------
1455+
Series.append
1456+
DataFrame.append
1457+
DataFrame.join
1458+
DataFrame.merge
1459+
1460+
Examples
1461+
--------
1462+
Combine two ``Series``.
1463+
1464+
>>> s1 = pd.Series(['a', 'b'])
1465+
>>> s2 = pd.Series(['c', 'd'])
1466+
>>> pd.concat([s1, s2])
1467+
0 a
1468+
1 b
1469+
0 c
1470+
1 d
1471+
dtype: object
1472+
1473+
Clear the existing index and reset it in the result
1474+
by setting the ``ignore_index`` option to ``True``.
1475+
1476+
>>> pd.concat([s1, s2], ignore_index=True)
1477+
0 a
1478+
1 b
1479+
2 c
1480+
3 d
1481+
dtype: object
1482+
1483+
Add a hierarchical index at the outermost level of
1484+
the data with the ``keys`` option.
1485+
1486+
>>> pd.concat([s1, s2], keys=['s1', 's2',])
1487+
s1 0 a
1488+
1 b
1489+
s2 0 c
1490+
1 d
1491+
dtype: object
1492+
1493+
Label the index keys you create with the ``names`` option.
1494+
1495+
>>> pd.concat([s1, s2], keys=['s1', 's2'],
1496+
... names=['Series name', 'Row ID'])
1497+
Series name Row ID
1498+
s1 0 a
1499+
1 b
1500+
s2 0 c
1501+
1 d
1502+
dtype: object
1503+
1504+
Combine two ``DataFrame`` objects with identical columns.
1505+
1506+
>>> df1 = pd.DataFrame([['a', 1], ['b', 2]],
1507+
... columns=['letter', 'number'])
1508+
>>> df1
1509+
letter number
1510+
0 a 1
1511+
1 b 2
1512+
>>> df2 = pd.DataFrame([['c', 3], ['d', 4]],
1513+
... columns=['letter', 'number'])
1514+
>>> df2
1515+
letter number
1516+
0 c 3
1517+
1 d 4
1518+
>>> pd.concat([df1, df2])
1519+
letter number
1520+
0 a 1
1521+
1 b 2
1522+
0 c 3
1523+
1 d 4
1524+
1525+
Combine ``DataFrame`` objects with overlapping columns
1526+
and return everything. Columns outside the intersection will
1527+
be filled with ``NaN`` values.
1528+
1529+
>>> df3 = pd.DataFrame([['c', 3, 'cat'], ['d', 4, 'dog']],
1530+
... columns=['letter', 'number', 'animal'])
1531+
>>> df3
1532+
letter number animal
1533+
0 c 3 cat
1534+
1 d 4 dog
1535+
>>> pd.concat([df1, df3])
1536+
animal letter number
1537+
0 NaN a 1
1538+
1 NaN b 2
1539+
0 cat c 3
1540+
1 dog d 4
1541+
1542+
Combine ``DataFrame`` objects with overlapping columns
1543+
and return only those that are shared by passing ``inner`` to
1544+
the ``join`` keyword argument.
1545+
1546+
>>> pd.concat([df1, df3], join="inner")
1547+
letter number
1548+
0 a 1
1549+
1 b 2
1550+
0 c 3
1551+
1 d 4
1552+
1553+
Combine ``DataFrame`` objects horizontally along the x axis by
1554+
passing in ``axis=1``.
1555+
1556+
>>> df4 = pd.DataFrame([['bird', 'polly'], ['monkey', 'george']],
1557+
... columns=['animal', 'name'])
1558+
>>> pd.concat([df1, df4], axis=1)
1559+
letter number animal name
1560+
0 a 1 bird polly
1561+
1 b 2 monkey george
1562+
1563+
Prevent the result from including duplicate index values with the
1564+
``verify_integrity`` option.
1565+
1566+
>>> df5 = pd.DataFrame([1], index=['a'])
1567+
>>> df5
1568+
0
1569+
a 1
1570+
>>> df6 = pd.DataFrame([2], index=['a'])
1571+
>>> df6
1572+
0
1573+
a 2
1574+
>>> pd.concat([df5, df6], verify_integrity=True)
1575+
ValueError: Indexes have overlapping values: ['a']
14461576
"""
14471577
op = _Concatenator(objs, axis=axis, join_axes=join_axes,
14481578
ignore_index=ignore_index, join=join,

0 commit comments

Comments
 (0)