1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
|
.. _merging:
{{ header }}
.. ipython:: python
:suppress:
from matplotlib import pyplot as plt
import pandas.util._doctools as doctools
p = doctools.TablePlotter()
************************************
Merge, join, concatenate and compare
************************************
pandas provides various facilities for easily combining together Series or
DataFrame with various kinds of set logic for the indexes
and relational algebra functionality in the case of join / merge-type
operations.
In addition, pandas also provides utilities to compare two Series or DataFrame
and summarize their differences.
.. _merging.concat:
Concatenating objects
---------------------
The :func:`~pandas.concat` function (in the main pandas namespace) does all of
the heavy lifting of performing concatenation operations along an axis while
performing optional set logic (union or intersection) of the indexes (if any) on
the other axes. Note that I say "if any" because there is only a single possible
axis of concatenation for Series.
Before diving into all of the details of ``concat`` and what it can do, here is
a simple example:
.. ipython:: python
df1 = pd.DataFrame(
{
"A": ["A0", "A1", "A2", "A3"],
"B": ["B0", "B1", "B2", "B3"],
"C": ["C0", "C1", "C2", "C3"],
"D": ["D0", "D1", "D2", "D3"],
},
index=[0, 1, 2, 3],
)
df2 = pd.DataFrame(
{
"A": ["A4", "A5", "A6", "A7"],
"B": ["B4", "B5", "B6", "B7"],
"C": ["C4", "C5", "C6", "C7"],
"D": ["D4", "D5", "D6", "D7"],
},
index=[4, 5, 6, 7],
)
df3 = pd.DataFrame(
{
"A": ["A8", "A9", "A10", "A11"],
"B": ["B8", "B9", "B10", "B11"],
"C": ["C8", "C9", "C10", "C11"],
"D": ["D8", "D9", "D10", "D11"],
},
index=[8, 9, 10, 11],
)
frames = [df1, df2, df3]
result = pd.concat(frames)
.. ipython:: python
:suppress:
@savefig merging_concat_basic.png
p.plot(frames, result, labels=["df1", "df2", "df3"], vertical=True);
plt.close("all");
Like its sibling function on ndarrays, ``numpy.concatenate``, ``pandas.concat``
takes a list or dict of homogeneously-typed objects and concatenates them with
some configurable handling of "what to do with the other axes":
::
pd.concat(
objs,
axis=0,
join="outer",
ignore_index=False,
keys=None,
levels=None,
names=None,
verify_integrity=False,
copy=True,
)
* ``objs`` : a sequence or mapping of Series or DataFrame objects. If a
dict is passed, the sorted keys will be used as the ``keys`` argument, unless
it is passed, in which case the values will be selected (see below). Any None
objects will be dropped silently unless they are all None in which case a
ValueError will be raised.
* ``axis`` : {0, 1, ...}, default 0. The axis to concatenate along.
* ``join`` : {'inner', 'outer'}, default 'outer'. How to handle indexes on
other axis(es). Outer for union and inner for intersection.
* ``ignore_index`` : boolean, default False. If True, do not use the index
values on the concatenation axis. The resulting axis will be labeled 0, ...,
n - 1. This is useful if you are concatenating objects where the
concatenation axis does not have meaningful indexing information. Note
the index values on the other axes are still respected in the join.
* ``keys`` : sequence, default None. Construct hierarchical index using the
passed keys as the outermost level. If multiple levels passed, should
contain tuples.
* ``levels`` : list of sequences, default None. Specific levels (unique values)
to use for constructing a MultiIndex. Otherwise they will be inferred from the
keys.
* ``names`` : list, default None. Names for the levels in the resulting
hierarchical index.
* ``verify_integrity`` : boolean, default False. Check whether the new
concatenated axis contains duplicates. This can be very expensive relative
to the actual data concatenation.
* ``copy`` : boolean, default True. If False, do not copy data unnecessarily.
Without a little bit of context many of these arguments don't make much sense.
Let's revisit the above example. Suppose we wanted to associate specific keys
with each of the pieces of the chopped up DataFrame. We can do this using the
``keys`` argument:
.. ipython:: python
result = pd.concat(frames, keys=["x", "y", "z"])
.. ipython:: python
:suppress:
@savefig merging_concat_keys.png
p.plot(frames, result, labels=["df1", "df2", "df3"], vertical=True)
plt.close("all");
As you can see (if you've read the rest of the documentation), the resulting
object's index has a :ref:`hierarchical index <advanced.hierarchical>`. This
means that we can now select out each chunk by key:
.. ipython:: python
result.loc["y"]
It's not a stretch to see how this can be very useful. More detail on this
functionality below.
.. note::
It is worth noting that :func:`~pandas.concat` (and therefore
:func:`~pandas.append`) makes a full copy of the data, and that constantly
reusing this function can create a significant performance hit. If you need
to use the operation over several datasets, use a list comprehension.
::
frames = [ process_your_file(f) for f in files ]
result = pd.concat(frames)
.. note::
When concatenating DataFrames with named axes, pandas will attempt to preserve
these index/column names whenever possible. In the case where all inputs share a
common name, this name will be assigned to the result. When the input names do
not all agree, the result will be unnamed. The same is true for :class:`MultiIndex`,
but the logic is applied separately on a level-by-level basis.
Set logic on the other axes
~~~~~~~~~~~~~~~~~~~~~~~~~~~
When gluing together multiple DataFrames, you have a choice of how to handle
the other axes (other than the one being concatenated). This can be done in
the following two ways:
* Take the union of them all, ``join='outer'``. This is the default
option as it results in zero information loss.
* Take the intersection, ``join='inner'``.
Here is an example of each of these methods. First, the default ``join='outer'``
behavior:
.. ipython:: python
df4 = pd.DataFrame(
{
"B": ["B2", "B3", "B6", "B7"],
"D": ["D2", "D3", "D6", "D7"],
"F": ["F2", "F3", "F6", "F7"],
},
index=[2, 3, 6, 7],
)
result = pd.concat([df1, df4], axis=1)
.. ipython:: python
:suppress:
@savefig merging_concat_axis1.png
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=False);
plt.close("all");
Here is the same thing with ``join='inner'``:
.. ipython:: python
result = pd.concat([df1, df4], axis=1, join="inner")
.. ipython:: python
:suppress:
@savefig merging_concat_axis1_inner.png
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=False);
plt.close("all");
Lastly, suppose we just wanted to reuse the *exact index* from the original
DataFrame:
.. ipython:: python
result = pd.concat([df1, df4], axis=1).reindex(df1.index)
Similarly, we could index before the concatenation:
.. ipython:: python
pd.concat([df1, df4.reindex(df1.index)], axis=1)
.. ipython:: python
:suppress:
@savefig merging_concat_axis1_join_axes.png
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=False);
plt.close("all");
.. _merging.ignore_index:
Ignoring indexes on the concatenation axis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For ``DataFrame`` objects which don't have a meaningful index, you may wish
to append them and ignore the fact that they may have overlapping indexes. To
do this, use the ``ignore_index`` argument:
.. ipython:: python
result = pd.concat([df1, df4], ignore_index=True, sort=False)
.. ipython:: python
:suppress:
@savefig merging_concat_ignore_index.png
p.plot([df1, df4], result, labels=["df1", "df4"], vertical=True);
plt.close("all");
.. _merging.mixed_ndims:
Concatenating with mixed ndims
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can concatenate a mix of ``Series`` and ``DataFrame`` objects. The
``Series`` will be transformed to ``DataFrame`` with the column name as
the name of the ``Series``.
.. ipython:: python
s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X")
result = pd.concat([df1, s1], axis=1)
.. ipython:: python
:suppress:
@savefig merging_concat_mixed_ndim.png
p.plot([df1, s1], result, labels=["df1", "s1"], vertical=False);
plt.close("all");
.. note::
Since we're concatenating a ``Series`` to a ``DataFrame``, we could have
achieved the same result with :meth:`DataFrame.assign`. To concatenate an
arbitrary number of pandas objects (``DataFrame`` or ``Series``), use
``concat``.
If unnamed ``Series`` are passed they will be numbered consecutively.
.. ipython:: python
s2 = pd.Series(["_0", "_1", "_2", "_3"])
result = pd.concat([df1, s2, s2, s2], axis=1)
.. ipython:: python
:suppress:
@savefig merging_concat_unnamed_series.png
p.plot([df1, s2], result, labels=["df1", "s2"], vertical=False);
plt.close("all");
Passing ``ignore_index=True`` will drop all name references.
.. ipython:: python
result = pd.concat([df1, s1], axis=1, ignore_index=True)
.. ipython:: python
:suppress:
@savefig merging_concat_series_ignore_index.png
p.plot([df1, s1], result, labels=["df1", "s1"], vertical=False);
plt.close("all");
More concatenating with group keys
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A fairly common use of the ``keys`` argument is to override the column names
when creating a new ``DataFrame`` based on existing ``Series``.
Notice how the default behaviour consists on letting the resulting ``DataFrame``
inherit the parent ``Series``' name, when these existed.
.. ipython:: python
s3 = pd.Series([0, 1, 2, 3], name="foo")
s4 = pd.Series([0, 1, 2, 3])
s5 = pd.Series([0, 1, 4, 5])
pd.concat([s3, s4, s5], axis=1)
Through the ``keys`` argument we can override the existing column names.
.. ipython:: python
pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"])
Let's consider a variation of the very first example presented:
.. ipython:: python
result = pd.concat(frames, keys=["x", "y", "z"])
.. ipython:: python
:suppress:
@savefig merging_concat_group_keys2.png
p.plot(frames, result, labels=["df1", "df2", "df3"], vertical=True);
plt.close("all");
You can also pass a dict to ``concat`` in which case the dict keys will be used
for the ``keys`` argument (unless other keys are specified):
.. ipython:: python
pieces = {"x": df1, "y": df2, "z": df3}
result = pd.concat(pieces)
.. ipython:: python
:suppress:
@savefig merging_concat_dict.png
p.plot([df1, df2, df3], result, labels=["df1", "df2", "df3"], vertical=True);
plt.close("all");
.. ipython:: python
result = pd.concat(pieces, keys=["z", "y"])
.. ipython:: python
:suppress:
@savefig merging_concat_dict_keys.png
p.plot([df1, df2, df3], result, labels=["df1", "df2", "df3"], vertical=True);
plt.close("all");
The MultiIndex created has levels that are constructed from the passed keys and
the index of the ``DataFrame`` pieces:
.. ipython:: python
result.index.levels
If you wish to specify other levels (as will occasionally be the case), you can
do so using the ``levels`` argument:
.. ipython:: python
result = pd.concat(
pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"]
)
.. ipython:: python
:suppress:
@savefig merging_concat_dict_keys_names.png
p.plot([df1, df2, df3], result, labels=["df1", "df2", "df3"], vertical=True);
plt.close("all");
.. ipython:: python
result.index.levels
This is fairly esoteric, but it is actually necessary for implementing things
like GroupBy where the order of a categorical variable is meaningful.
.. _merging.append.row:
Appending rows to a DataFrame
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you have a series that you want to append as a single row to a ``DataFrame``, you can convert the row into a
``DataFrame`` and use ``concat``
.. ipython:: python
s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"])
result = pd.concat([df1, s2.to_frame().T], ignore_index=True)
.. ipython:: python
:suppress:
@savefig merging_append_series_as_row.png
p.plot([df1, s2], result, labels=["df1", "s2"], vertical=True);
plt.close("all");
You should use ``ignore_index`` with this method to instruct DataFrame to
discard its index. If you wish to preserve the index, you should construct an
appropriately-indexed DataFrame and append or concatenate those objects.
.. _merging.join:
Database-style DataFrame or named Series joining/merging
--------------------------------------------------------
pandas has full-featured, **high performance** in-memory join operations
idiomatically very similar to relational databases like SQL. These methods
perform significantly better (in some cases well over an order of magnitude
better) than other open source implementations (like ``base::merge.data.frame``
in R). The reason for this is careful algorithmic design and the internal layout
of the data in ``DataFrame``.
See the :ref:`cookbook<cookbook.merge>` for some advanced strategies.
Users who are familiar with SQL but new to pandas might be interested in a
:ref:`comparison with SQL<compare_with_sql.join>`.
pandas provides a single function, :func:`~pandas.merge`, as the entry point for
all standard database join operations between ``DataFrame`` or named ``Series`` objects:
::
pd.merge(
left,
right,
how="inner",
on=None,
left_on=None,
right_on=None,
left_index=False,
right_index=False,
sort=True,
suffixes=("_x", "_y"),
copy=True,
indicator=False,
validate=None,
)
* ``left``: A DataFrame or named Series object.
* ``right``: Another DataFrame or named Series object.
* ``on``: Column or index level names to join on. Must be found in both the left
and right DataFrame and/or Series objects. If not passed and ``left_index`` and
``right_index`` are ``False``, the intersection of the columns in the
DataFrames and/or Series will be inferred to be the join keys.
* ``left_on``: Columns or index levels from the left DataFrame or Series to use as
keys. Can either be column names, index level names, or arrays with length
equal to the length of the DataFrame or Series.
* ``right_on``: Columns or index levels from the right DataFrame or Series to use as
keys. Can either be column names, index level names, or arrays with length
equal to the length of the DataFrame or Series.
* ``left_index``: If ``True``, use the index (row labels) from the left
DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex
(hierarchical), the number of levels must match the number of join keys
from the right DataFrame or Series.
* ``right_index``: Same usage as ``left_index`` for the right DataFrame or Series
* ``how``: One of ``'left'``, ``'right'``, ``'outer'``, ``'inner'``, ``'cross'``. Defaults
to ``inner``. See below for more detailed description of each method.
* ``sort``: Sort the result DataFrame by the join keys in lexicographical
order. Defaults to ``True``, setting to ``False`` will improve performance
substantially in many cases.
* ``suffixes``: A tuple of string suffixes to apply to overlapping
columns. Defaults to ``('_x', '_y')``.
* ``copy``: Always copy data (default ``True``) from the passed DataFrame or named Series
objects, even when reindexing is not necessary. Cannot be avoided in many
cases but may improve performance / memory usage. The cases where copying
can be avoided are somewhat pathological but this option is provided
nonetheless.
* ``indicator``: Add a column to the output DataFrame called ``_merge``
with information on the source of each row. ``_merge`` is Categorical-type
and takes on a value of ``left_only`` for observations whose merge key
only appears in ``'left'`` DataFrame or Series, ``right_only`` for observations whose
merge key only appears in ``'right'`` DataFrame or Series, and ``both`` if the
observation's merge key is found in both.
* ``validate`` : string, default None.
If specified, checks if merge is of specified type.
* "one_to_one" or "1:1": checks if merge keys are unique in both
left and right datasets.
* "one_to_many" or "1:m": checks if merge keys are unique in left
dataset.
* "many_to_one" or "m:1": checks if merge keys are unique in right
dataset.
* "many_to_many" or "m:m": allowed, but does not result in checks.
.. note::
Support for specifying index levels as the ``on``, ``left_on``, and
``right_on`` parameters was added in version 0.23.0.
Support for merging named ``Series`` objects was added in version 0.24.0.
The return type will be the same as ``left``. If ``left`` is a ``DataFrame`` or named ``Series``
and ``right`` is a subclass of ``DataFrame``, the return type will still be ``DataFrame``.
``merge`` is a function in the pandas namespace, and it is also available as a
``DataFrame`` instance method :meth:`~DataFrame.merge`, with the calling
``DataFrame`` being implicitly considered the left object in the join.
The related :meth:`~DataFrame.join` method, uses ``merge`` internally for the
index-on-index (by default) and column(s)-on-index join. If you are joining on
index only, you may wish to use ``DataFrame.join`` to save yourself some typing.
Brief primer on merge methods (relational algebra)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Experienced users of relational databases like SQL will be familiar with the
terminology used to describe join operations between two SQL-table like
structures (``DataFrame`` objects). There are several cases to consider which
are very important to understand:
* **one-to-one** joins: for example when joining two ``DataFrame`` objects on
their indexes (which must contain unique values).
* **many-to-one** joins: for example when joining an index (unique) to one or
more columns in a different ``DataFrame``.
* **many-to-many** joins: joining columns on columns.
.. note::
When joining columns on columns (potentially a many-to-many join), any
indexes on the passed ``DataFrame`` objects **will be discarded**.
It is worth spending some time understanding the result of the **many-to-many**
join case. In SQL / standard relational algebra, if a key combination appears
more than once in both tables, the resulting table will have the **Cartesian
product** of the associated data. Here is a very basic example with one unique
key combination:
.. ipython:: python
left = pd.DataFrame(
{
"key": ["K0", "K1", "K2", "K3"],
"A": ["A0", "A1", "A2", "A3"],
"B": ["B0", "B1", "B2", "B3"],
}
)
right = pd.DataFrame(
{
"key": ["K0", "K1", "K2", "K3"],
"C": ["C0", "C1", "C2", "C3"],
"D": ["D0", "D1", "D2", "D3"],
}
)
result = pd.merge(left, right, on="key")
.. ipython:: python
:suppress:
@savefig merging_merge_on_key.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
Here is a more complicated example with multiple join keys. Only the keys
appearing in ``left`` and ``right`` are present (the intersection), since
``how='inner'`` by default.
.. ipython:: python
left = pd.DataFrame(
{
"key1": ["K0", "K0", "K1", "K2"],
"key2": ["K0", "K1", "K0", "K1"],
"A": ["A0", "A1", "A2", "A3"],
"B": ["B0", "B1", "B2", "B3"],
}
)
right = pd.DataFrame(
{
"key1": ["K0", "K1", "K1", "K2"],
"key2": ["K0", "K0", "K0", "K0"],
"C": ["C0", "C1", "C2", "C3"],
"D": ["D0", "D1", "D2", "D3"],
}
)
result = pd.merge(left, right, on=["key1", "key2"])
.. ipython:: python
:suppress:
@savefig merging_merge_on_key_multiple.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
The ``how`` argument to ``merge`` specifies how to determine which keys are to
be included in the resulting table. If a key combination **does not appear** in
either the left or right tables, the values in the joined table will be
``NA``. Here is a summary of the ``how`` options and their SQL equivalent names:
.. csv-table::
:header: "Merge method", "SQL Join Name", "Description"
:widths: 20, 20, 60
``left``, ``LEFT OUTER JOIN``, Use keys from left frame only
``right``, ``RIGHT OUTER JOIN``, Use keys from right frame only
``outer``, ``FULL OUTER JOIN``, Use union of keys from both frames
``inner``, ``INNER JOIN``, Use intersection of keys from both frames
``cross``, ``CROSS JOIN``, Create the cartesian product of rows of both frames
.. ipython:: python
result = pd.merge(left, right, how="left", on=["key1", "key2"])
.. ipython:: python
:suppress:
@savefig merging_merge_on_key_left.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. ipython:: python
result = pd.merge(left, right, how="right", on=["key1", "key2"])
.. ipython:: python
:suppress:
@savefig merging_merge_on_key_right.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
.. ipython:: python
result = pd.merge(left, right, how="outer", on=["key1", "key2"])
.. ipython:: python
:suppress:
@savefig merging_merge_on_key_outer.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. ipython:: python
result = pd.merge(left, right, how="inner", on=["key1", "key2"])
.. ipython:: python
:suppress:
@savefig merging_merge_on_key_inner.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. ipython:: python
result = pd.merge(left, right, how="cross")
.. ipython:: python
:suppress:
@savefig merging_merge_cross.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
You can merge a mult-indexed Series and a DataFrame, if the names of
the MultiIndex correspond to the columns from the DataFrame. Transform
the Series to a DataFrame using :meth:`Series.reset_index` before merging,
as shown in the following example.
.. ipython:: python
df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]})
df
ser = pd.Series(
["a", "b", "c", "d", "e", "f"],
index=pd.MultiIndex.from_arrays(
[["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"]
),
)
ser
pd.merge(df, ser.reset_index(), on=["Let", "Num"])
Here is another example with duplicate join keys in DataFrames:
.. ipython:: python
left = pd.DataFrame({"A": [1, 2], "B": [2, 2]})
right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]})
result = pd.merge(left, right, on="B", how="outer")
.. ipython:: python
:suppress:
@savefig merging_merge_on_key_dup.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. warning::
Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user' s responsibility to manage duplicate values in keys before joining large DataFrames.
.. _merging.validation:
Checking for duplicate keys
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Users can use the ``validate`` argument to automatically check whether there
are unexpected duplicates in their merge keys. Key uniqueness is checked before
merge operations and so should protect against memory overflows. Checking key
uniqueness is also a good way to ensure user data structures are as expected.
In the following example, there are duplicate values of ``B`` in the right
``DataFrame``. As this is not a one-to-one merge -- as specified in the
``validate`` argument -- an exception will be raised.
.. ipython:: python
left = pd.DataFrame({"A": [1, 2], "B": [1, 2]})
right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]})
.. code-block:: ipython
In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one")
...
MergeError: Merge keys are not unique in right dataset; not a one-to-one merge
If the user is aware of the duplicates in the right ``DataFrame`` but wants to
ensure there are no duplicates in the left DataFrame, one can use the
``validate='one_to_many'`` argument instead, which will not raise an exception.
.. ipython:: python
pd.merge(left, right, on="B", how="outer", validate="one_to_many")
.. _merging.indicator:
The merge indicator
~~~~~~~~~~~~~~~~~~~
:func:`~pandas.merge` accepts the argument ``indicator``. If ``True``, a
Categorical-type column called ``_merge`` will be added to the output object
that takes on values:
=================================== ================
Observation Origin ``_merge`` value
=================================== ================
Merge key only in ``'left'`` frame ``left_only``
Merge key only in ``'right'`` frame ``right_only``
Merge key in both frames ``both``
=================================== ================
.. ipython:: python
df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]})
df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]})
pd.merge(df1, df2, on="col1", how="outer", indicator=True)
The ``indicator`` argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column.
.. ipython:: python
pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column")
.. _merging.dtypes:
Merge dtypes
~~~~~~~~~~~~
Merging will preserve the dtype of the join keys.
.. ipython:: python
left = pd.DataFrame({"key": [1], "v1": [10]})
left
right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]})
right
We are able to preserve the join keys:
.. ipython:: python
pd.merge(left, right, how="outer")
pd.merge(left, right, how="outer").dtypes
Of course if you have missing values that are introduced, then the
resulting dtype will be upcast.
.. ipython:: python
pd.merge(left, right, how="outer", on="key")
pd.merge(left, right, how="outer", on="key").dtypes
Merging will preserve ``category`` dtypes of the mergands. See also the section on :ref:`categoricals <categorical.merge>`.
The left frame.
.. ipython:: python
from pandas.api.types import CategoricalDtype
X = pd.Series(np.random.choice(["foo", "bar"], size=(10,)))
X = X.astype(CategoricalDtype(categories=["foo", "bar"]))
left = pd.DataFrame(
{"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))}
)
left
left.dtypes
The right frame.
.. ipython:: python
right = pd.DataFrame(
{
"X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])),
"Z": [1, 2],
}
)
right
right.dtypes
The merged result:
.. ipython:: python
result = pd.merge(left, right, how="outer")
result
result.dtypes
.. note::
The category dtypes must be *exactly* the same, meaning the same categories and the ordered attribute.
Otherwise the result will coerce to the categories' dtype.
.. note::
Merging on ``category`` dtypes that are the same can be quite performant compared to ``object`` dtype merging.
.. _merging.join.index:
Joining on index
~~~~~~~~~~~~~~~~
:meth:`DataFrame.join` is a convenient method for combining the columns of two
potentially differently-indexed ``DataFrames`` into a single result
``DataFrame``. Here is a very basic example:
.. ipython:: python
left = pd.DataFrame(
{"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"]
)
right = pd.DataFrame(
{"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"]
)
result = left.join(right)
.. ipython:: python
:suppress:
@savefig merging_join.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. ipython:: python
result = left.join(right, how="outer")
.. ipython:: python
:suppress:
@savefig merging_join_outer.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
The same as above, but with ``how='inner'``.
.. ipython:: python
result = left.join(right, how="inner")
.. ipython:: python
:suppress:
@savefig merging_join_inner.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
The data alignment here is on the indexes (row labels). This same behavior can
be achieved using ``merge`` plus additional arguments instructing it to use the
indexes:
.. ipython:: python
result = pd.merge(left, right, left_index=True, right_index=True, how="outer")
.. ipython:: python
:suppress:
@savefig merging_merge_index_outer.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. ipython:: python
result = pd.merge(left, right, left_index=True, right_index=True, how="inner")
.. ipython:: python
:suppress:
@savefig merging_merge_index_inner.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
Joining key columns on an index
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:meth:`~DataFrame.join` takes an optional ``on`` argument which may be a column
or multiple column names, which specifies that the passed ``DataFrame`` is to be
aligned on that column in the ``DataFrame``. These two function calls are
completely equivalent:
::
left.join(right, on=key_or_keys)
pd.merge(
left, right, left_on=key_or_keys, right_index=True, how="left", sort=False
)
Obviously you can choose whichever form you find more convenient. For
many-to-one joins (where one of the ``DataFrame``'s is already indexed by the
join key), using ``join`` may be more convenient. Here is a simple example:
.. ipython:: python
left = pd.DataFrame(
{
"A": ["A0", "A1", "A2", "A3"],
"B": ["B0", "B1", "B2", "B3"],
"key": ["K0", "K1", "K0", "K1"],
}
)
right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"])
result = left.join(right, on="key")
.. ipython:: python
:suppress:
@savefig merging_join_key_columns.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. ipython:: python
result = pd.merge(
left, right, left_on="key", right_index=True, how="left", sort=False
)
.. ipython:: python
:suppress:
@savefig merging_merge_key_columns.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. _merging.multikey_join:
To join on multiple keys, the passed DataFrame must have a ``MultiIndex``:
.. ipython:: python
left = pd.DataFrame(
{
"A": ["A0", "A1", "A2", "A3"],
"B": ["B0", "B1", "B2", "B3"],
"key1": ["K0", "K0", "K1", "K2"],
"key2": ["K0", "K1", "K0", "K1"],
}
)
index = pd.MultiIndex.from_tuples(
[("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")]
)
right = pd.DataFrame(
{"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index
)
Now this can be joined by passing the two key column names:
.. ipython:: python
result = left.join(right, on=["key1", "key2"])
.. ipython:: python
:suppress:
@savefig merging_join_multikeys.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. _merging.df_inner_join:
The default for ``DataFrame.join`` is to perform a left join (essentially a
"VLOOKUP" operation, for Excel users), which uses only the keys found in the
calling DataFrame. Other join types, for example inner join, can be just as
easily performed:
.. ipython:: python
result = left.join(right, on=["key1", "key2"], how="inner")
.. ipython:: python
:suppress:
@savefig merging_join_multikeys_inner.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
As you can see, this drops any rows where there was no match.
.. _merging.join_on_mi:
Joining a single Index to a MultiIndex
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can join a singly-indexed ``DataFrame`` with a level of a MultiIndexed ``DataFrame``.
The level will match on the name of the index of the singly-indexed frame against
a level name of the MultiIndexed frame.
.. ipython:: python
left = pd.DataFrame(
{"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]},
index=pd.Index(["K0", "K1", "K2"], name="key"),
)
index = pd.MultiIndex.from_tuples(
[("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")],
names=["key", "Y"],
)
right = pd.DataFrame(
{"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]},
index=index,
)
result = left.join(right, how="inner")
.. ipython:: python
:suppress:
@savefig merging_join_multiindex_inner.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
This is equivalent but less verbose and more memory efficient / faster than this.
.. ipython:: python
result = pd.merge(
left.reset_index(), right.reset_index(), on=["key"], how="inner"
).set_index(["key","Y"])
.. ipython:: python
:suppress:
@savefig merging_merge_multiindex_alternative.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. _merging.join_with_two_multi_indexes:
Joining with two MultiIndexes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is supported in a limited way, provided that the index for the right
argument is completely used in the join, and is a subset of the indices in
the left argument, as in this example:
.. ipython:: python
leftindex = pd.MultiIndex.from_product(
[list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"]
)
left = pd.DataFrame({"v1": range(12)}, index=leftindex)
left
rightindex = pd.MultiIndex.from_product(
[list("abc"), list("xy")], names=["abc", "xy"]
)
right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex)
right
left.join(right, on=["abc", "xy"], how="inner")
If that condition is not satisfied, a join with two multi-indexes can be
done using the following code.
.. ipython:: python
leftindex = pd.MultiIndex.from_tuples(
[("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"]
)
left = pd.DataFrame(
{"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex
)
rightindex = pd.MultiIndex.from_tuples(
[("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"]
)
right = pd.DataFrame(
{"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex
)
result = pd.merge(
left.reset_index(), right.reset_index(), on=["key"], how="inner"
).set_index(["key", "X", "Y"])
.. ipython:: python
:suppress:
@savefig merging_merge_two_multiindex.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. _merging.merge_on_columns_and_levels:
Merging on a combination of columns and index levels
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Strings passed as the ``on``, ``left_on``, and ``right_on`` parameters
may refer to either column names or index level names. This enables merging
``DataFrame`` instances on a combination of index levels and columns without
resetting indexes.
.. ipython:: python
left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1")
left = pd.DataFrame(
{
"A": ["A0", "A1", "A2", "A3"],
"B": ["B0", "B1", "B2", "B3"],
"key2": ["K0", "K1", "K0", "K1"],
},
index=left_index,
)
right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1")
right = pd.DataFrame(
{
"C": ["C0", "C1", "C2", "C3"],
"D": ["D0", "D1", "D2", "D3"],
"key2": ["K0", "K0", "K0", "K1"],
},
index=right_index,
)
result = left.merge(right, on=["key1", "key2"])
.. ipython:: python
:suppress:
@savefig merge_on_index_and_column.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. note::
When DataFrames are merged on a string that matches an index level in both
frames, the index level is preserved as an index level in the resulting
DataFrame.
.. note::
When DataFrames are merged using only some of the levels of a ``MultiIndex``,
the extra levels will be dropped from the resulting merge. In order to
preserve those levels, use ``reset_index`` on those level names to move
those levels to columns prior to doing the merge.
.. note::
If a string matches both a column name and an index level name, then a
warning is issued and the column takes precedence. This will result in an
ambiguity error in a future version.
Overlapping value columns
~~~~~~~~~~~~~~~~~~~~~~~~~
The merge ``suffixes`` argument takes a tuple of list of strings to append to
overlapping column names in the input ``DataFrame``\ s to disambiguate the result
columns:
.. ipython:: python
left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]})
right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]})
result = pd.merge(left, right, on="k")
.. ipython:: python
:suppress:
@savefig merging_merge_overlapped.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. ipython:: python
result = pd.merge(left, right, on="k", suffixes=("_l", "_r"))
.. ipython:: python
:suppress:
@savefig merging_merge_overlapped_suffix.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
:meth:`DataFrame.join` has ``lsuffix`` and ``rsuffix`` arguments which behave
similarly.
.. ipython:: python
left = left.set_index("k")
right = right.set_index("k")
result = left.join(right, lsuffix="_l", rsuffix="_r")
.. ipython:: python
:suppress:
@savefig merging_merge_overlapped_multi_suffix.png
p.plot([left, right], result, labels=["left", "right"], vertical=False);
plt.close("all");
.. _merging.multiple_join:
Joining multiple DataFrames
~~~~~~~~~~~~~~~~~~~~~~~~~~~
A list or tuple of ``DataFrames`` can also be passed to :meth:`~DataFrame.join`
to join them together on their indexes.
.. ipython:: python
right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"])
result = left.join([right, right2])
.. ipython:: python
:suppress:
@savefig merging_join_multi_df.png
p.plot(
[left, right, right2],
result,
labels=["left", "right", "right2"],
vertical=False,
);
plt.close("all");
.. _merging.combine_first.update:
Merging together values within Series or DataFrame columns
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Another fairly common situation is to have two like-indexed (or similarly
indexed) ``Series`` or ``DataFrame`` objects and wanting to "patch" values in
one object from values for matching indices in the other. Here is an example:
.. ipython:: python
df1 = pd.DataFrame(
[[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]]
)
df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2])
For this, use the :meth:`~DataFrame.combine_first` method:
.. ipython:: python
result = df1.combine_first(df2)
.. ipython:: python
:suppress:
@savefig merging_combine_first.png
p.plot([df1, df2], result, labels=["df1", "df2"], vertical=False);
plt.close("all");
Note that this method only takes values from the right ``DataFrame`` if they are
missing in the left ``DataFrame``. A related method, :meth:`~DataFrame.update`,
alters non-NA values in place:
.. ipython:: python
:suppress:
df1_copy = df1.copy()
.. ipython:: python
df1.update(df2)
.. ipython:: python
:suppress:
@savefig merging_update.png
p.plot([df1_copy, df2], df1, labels=["df1", "df2"], vertical=False);
plt.close("all");
.. _merging.time_series:
Timeseries friendly merging
---------------------------
.. _merging.merge_ordered:
Merging ordered data
~~~~~~~~~~~~~~~~~~~~
A :func:`merge_ordered` function allows combining time series and other
ordered data. In particular it has an optional ``fill_method`` keyword to
fill/interpolate missing data:
.. ipython:: python
left = pd.DataFrame(
{"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]}
)
right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]})
pd.merge_ordered(left, right, fill_method="ffill", left_by="s")
.. _merging.merge_asof:
Merging asof
~~~~~~~~~~~~
A :func:`merge_asof` is similar to an ordered left-join except that we match on
nearest key rather than equal keys. For each row in the ``left`` ``DataFrame``,
we select the last row in the ``right`` ``DataFrame`` whose ``on`` key is less
than the left's key. Both DataFrames must be sorted by the key.
Optionally an asof merge can perform a group-wise merge. This matches the
``by`` key equally, in addition to the nearest match on the ``on`` key.
For example; we might have ``trades`` and ``quotes`` and we want to ``asof``
merge them.
.. ipython:: python
trades = pd.DataFrame(
{
"time": pd.to_datetime(
[
"20160525 13:30:00.023",
"20160525 13:30:00.038",
"20160525 13:30:00.048",
"20160525 13:30:00.048",
"20160525 13:30:00.048",
]
),
"ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"],
"price": [51.95, 51.95, 720.77, 720.92, 98.00],
"quantity": [75, 155, 100, 100, 100],
},
columns=["time", "ticker", "price", "quantity"],
)
quotes = pd.DataFrame(
{
"time": pd.to_datetime(
[
"20160525 13:30:00.023",
"20160525 13:30:00.023",
"20160525 13:30:00.030",
"20160525 13:30:00.041",
"20160525 13:30:00.048",
"20160525 13:30:00.049",
"20160525 13:30:00.072",
"20160525 13:30:00.075",
]
),
"ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"],
"bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01],
"ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03],
},
columns=["time", "ticker", "bid", "ask"],
)
.. ipython:: python
trades
quotes
By default we are taking the asof of the quotes.
.. ipython:: python
pd.merge_asof(trades, quotes, on="time", by="ticker")
We only asof within ``2ms`` between the quote time and the trade time.
.. ipython:: python
pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms"))
We only asof within ``10ms`` between the quote time and the trade time and we
exclude exact matches on time. Note that though we exclude the exact matches
(of the quotes), prior quotes **do** propagate to that point in time.
.. ipython:: python
pd.merge_asof(
trades,
quotes,
on="time",
by="ticker",
tolerance=pd.Timedelta("10ms"),
allow_exact_matches=False,
)
.. _merging.compare:
Comparing objects
-----------------
The :meth:`~Series.compare` and :meth:`~DataFrame.compare` methods allow you to
compare two DataFrame or Series, respectively, and summarize their differences.
This feature was added in :ref:`V1.1.0 <whatsnew_110.dataframe_or_series_comparing>`.
For example, you might want to compare two ``DataFrame`` and stack their differences
side by side.
.. ipython:: python
df = pd.DataFrame(
{
"col1": ["a", "a", "b", "b", "a"],
"col2": [1.0, 2.0, 3.0, np.nan, 5.0],
"col3": [1.0, 2.0, 3.0, 4.0, 5.0],
},
columns=["col1", "col2", "col3"],
)
df
.. ipython:: python
df2 = df.copy()
df2.loc[0, "col1"] = "c"
df2.loc[2, "col3"] = 4.0
df2
.. ipython:: python
df.compare(df2)
By default, if two corresponding values are equal, they will be shown as ``NaN``.
Furthermore, if all values in an entire row / column, the row / column will be
omitted from the result. The remaining differences will be aligned on columns.
If you wish, you may choose to stack the differences on rows.
.. ipython:: python
df.compare(df2, align_axis=0)
If you wish to keep all original rows and columns, set ``keep_shape`` argument
to ``True``.
.. ipython:: python
df.compare(df2, keep_shape=True)
You may also keep all the original values even if they are equal.
.. ipython:: python
df.compare(df2, keep_shape=True, keep_equal=True)
|