|
509 | 509 | "Notice that `mmsi` field that was an index on the `vessels` table is no longer an index on the merged table." |
510 | 510 | ] |
511 | 511 | }, |
| 512 | + { |
| 513 | + "cell_type": "markdown", |
| 514 | + "metadata": {}, |
| 515 | + "source": [ |
| 516 | + "Here, we used the `merge` function to perform the merge; we could also have used the `merge` method for either of the tables:" |
| 517 | + ] |
| 518 | + }, |
| 519 | + { |
| 520 | + "cell_type": "code", |
| 521 | + "collapsed": false, |
| 522 | + "input": [ |
| 523 | + "vessels.merge(segments, left_index=True, right_on='mmsi').head()" |
| 524 | + ], |
| 525 | + "language": "python", |
| 526 | + "metadata": {}, |
| 527 | + "outputs": [] |
| 528 | + }, |
512 | 529 | { |
513 | 530 | "cell_type": "markdown", |
514 | 531 | "metadata": {}, |
|
588 | 605 | "cell_type": "code", |
589 | 606 | "collapsed": false, |
590 | 607 | "input": [ |
591 | | - "mb1 = pd.ExcelFile('data/microbiome/MID1.xls').parse(\"Sheet 1\", index_col=0, header=None, names=['Taxon', 'Count'])\n", |
592 | | - "mb2 = pd.ExcelFile('data/microbiome/MID2.xls').parse(\"Sheet 1\", index_col=0, header=None, names=['Taxon', 'Count'])\n", |
| 608 | + "mb1 = pd.read_excel('data/microbiome/MID1.xls', 'Sheet 1', index_col=0, header=None)\n", |
| 609 | + "mb2 = pd.read_excel('data/microbiome/MID2.xls', 'Sheet 1', index_col=0, header=None)\n", |
593 | 610 | "mb1.shape, mb2.shape" |
594 | 611 | ], |
595 | 612 | "language": "python", |
|
606 | 623 | "metadata": {}, |
607 | 624 | "outputs": [] |
608 | 625 | }, |
| 626 | + { |
| 627 | + "cell_type": "markdown", |
| 628 | + "metadata": {}, |
| 629 | + "source": [ |
| 630 | + "Let's give the index and columns meaningful labels:" |
| 631 | + ] |
| 632 | + }, |
609 | 633 | { |
610 | 634 | "cell_type": "code", |
611 | 635 | "collapsed": false, |
|
626 | 650 | "metadata": {}, |
627 | 651 | "outputs": [] |
628 | 652 | }, |
| 653 | + { |
| 654 | + "cell_type": "code", |
| 655 | + "collapsed": false, |
| 656 | + "input": [ |
| 657 | + "mb1.head()" |
| 658 | + ], |
| 659 | + "language": "python", |
| 660 | + "metadata": {}, |
| 661 | + "outputs": [] |
| 662 | + }, |
629 | 663 | { |
630 | 664 | "cell_type": "markdown", |
631 | 665 | "metadata": {}, |
|
754 | 788 | "cell_type": "code", |
755 | 789 | "collapsed": false, |
756 | 790 | "input": [ |
757 | | - "mb1.combine_first(mb2).shape" |
| 791 | + "mb1.combine_first(mb2).head()" |
758 | 792 | ], |
759 | 793 | "language": "python", |
760 | 794 | "metadata": {}, |
|
788 | 822 | "cell_type": "code", |
789 | 823 | "collapsed": false, |
790 | 824 | "input": [ |
791 | | - "pd.concat({'patient1': mb1, 'patient2': mb2}, axis=1).head()" |
| 825 | + "pd.concat(dict(patient1=mb1, patient2=mb2), axis=1).head()" |
792 | 826 | ], |
793 | 827 | "language": "python", |
794 | 828 | "metadata": {}, |
|
909 | 943 | "metadata": {}, |
910 | 944 | "outputs": [] |
911 | 945 | }, |
| 946 | + { |
| 947 | + "cell_type": "code", |
| 948 | + "collapsed": false, |
| 949 | + "input": [ |
| 950 | + "cdystonia2.index.is_unique" |
| 951 | + ], |
| 952 | + "language": "python", |
| 953 | + "metadata": {}, |
| 954 | + "outputs": [] |
| 955 | + }, |
912 | 956 | { |
913 | 957 | "cell_type": "markdown", |
914 | 958 | "metadata": {}, |
915 | 959 | "source": [ |
916 | | - "If we want to transform this data so that repeated measurements are in columns, we can `unstack` according to `obs`." |
| 960 | + "If we want to transform this data so that repeated measurements are in columns, we can `unstack` the `twstrs` measurements according to `obs`." |
917 | 961 | ] |
918 | 962 | }, |
919 | 963 | { |
920 | 964 | "cell_type": "code", |
921 | 965 | "collapsed": false, |
922 | 966 | "input": [ |
923 | | - "cdystonia2.unstack('obs')" |
| 967 | + "twstrs_wide = cdystonia2['twstrs'].unstack('obs')\n", |
| 968 | + "cdystonia.ix[:,:-1].merge(twstrs_wide, right_index=True, left_on='patient').head()" |
924 | 969 | ], |
925 | 970 | "language": "python", |
926 | 971 | "metadata": {}, |
|
950 | 995 | "cell_type": "code", |
951 | 996 | "collapsed": false, |
952 | 997 | "input": [ |
953 | | - "cdystonia.pivot_table?" |
954 | | - ], |
955 | | - "language": "python", |
956 | | - "metadata": {}, |
957 | | - "outputs": [] |
958 | | - }, |
959 | | - { |
960 | | - "cell_type": "code", |
961 | | - "collapsed": false, |
962 | | - "input": [ |
963 | | - "cdystonia.pivot('patient', 'obs', 'twstrs').head()" |
| 998 | + "cdystonia.pivot(index='patient', columns='obs', values='twstrs').head()" |
964 | 999 | ], |
965 | 1000 | "language": "python", |
966 | 1001 | "metadata": {}, |
|
970 | 1005 | "cell_type": "markdown", |
971 | 1006 | "metadata": {}, |
972 | 1007 | "source": [ |
973 | | - "If we omit the `value` argument, we get a `DataFrame` with hierarchical columns, just as when we applied `unstack` to the hierarchically-indexed table:" |
| 1008 | + "If we omit the `values` argument, we get a `DataFrame` with hierarchical columns, just as when we applied `unstack` to the hierarchically-indexed table:" |
974 | 1009 | ] |
975 | 1010 | }, |
976 | 1011 | { |
|
1329 | 1364 | "source": [ |
1330 | 1365 | "### Permutation and sampling\n", |
1331 | 1366 | "\n", |
1332 | | - "For some data analysis tasks, such as simulation, we need to be able to randomly reorder our data, or draw random values from it. Calling NumPy's `permutation` function with the length of the sequence you want to permute generates an array with a random array of integers, which can be used to re-order the sequence." |
| 1367 | + "For some data analysis tasks, such as simulation, we need to be able to randomly reorder our data, or draw random values from it. Calling NumPy's `permutation` function with the length of the sequence you want to permute generates an array with a permuted sequence of integers, which can be used to re-order the sequence." |
1333 | 1368 | ] |
1334 | 1369 | }, |
1335 | 1370 | { |
|
1376 | 1411 | "source": [ |
1377 | 1412 | "## Exercise\n", |
1378 | 1413 | "\n", |
1379 | | - "Use this permutation approach to draw a random sample (without replacement) of 5 ships from the `vessels` DataFrame." |
| 1414 | + "Its easy to see how this permutation approach allows us to draw a random sample **without replacement**. How would you sample **with replacement**? Generate a random sample of 5 ships from the `vessels` DataFrame using this scheme." |
1380 | 1415 | ] |
1381 | 1416 | }, |
1382 | 1417 | { |
|
1395 | 1430 | "\n", |
1396 | 1431 | "One of the most powerful features of Pandas is its **GroupBy** functionality. On occasion we may want to perform operations on *groups* of observations within a dataset. For exmaple:\n", |
1397 | 1432 | "\n", |
1398 | | - "* aggregation, such as computing the sum of mean of each group, which involves applying a function to each group and returning the aggregated results\n", |
1399 | | - "* slicing the DataFrame into groups and then doing something with the resulting slices (*e.g.* ploting)\n", |
1400 | | - "* group-wise transformation, such as standardization/normalization" |
| 1433 | + "* **aggregation**, such as computing the sum of mean of each group, which involves applying a function to each group and returning the aggregated results\n", |
| 1434 | + "* **slicing** the DataFrame into groups and then doing something with the resulting slices (*e.g.* plotting)\n", |
| 1435 | + "* group-wise **transformation**, such as standardization/normalization" |
1401 | 1436 | ] |
1402 | 1437 | }, |
1403 | 1438 | { |
|
1456 | 1491 | "\n", |
1457 | 1492 | "For example, we may want to aggregate our data with with some function.\n", |
1458 | 1493 | "\n", |
1459 | | - "" |
| 1494 | + "\n", |
| 1495 | + "\n", |
| 1496 | + "*(taken from \"Python for Data Analysis\")*" |
1460 | 1497 | ] |
1461 | 1498 | }, |
1462 | 1499 | { |
|
1516 | 1553 | "cell_type": "code", |
1517 | 1554 | "collapsed": false, |
1518 | 1555 | "input": [ |
| 1556 | + "# The median of the `twstrs` variable\n", |
1519 | 1557 | "cdystonia_grouped['twstrs'].quantile(0.5)" |
1520 | 1558 | ], |
1521 | 1559 | "language": "python", |
|
1678 | 1716 | "collapsed": false, |
1679 | 1717 | "input": [ |
1680 | 1718 | "def top(df, column, n=5):\n", |
1681 | | - " return df.sort_index(by=column)[-n:]" |
| 1719 | + " return df.sort_index(by=column, ascending=False)[:n]" |
1682 | 1720 | ], |
1683 | 1721 | "language": "python", |
1684 | 1722 | "metadata": {}, |
|
1781 | 1819 | "metadata": {}, |
1782 | 1820 | "outputs": [] |
1783 | 1821 | }, |
1784 | | - { |
1785 | | - "cell_type": "code", |
1786 | | - "collapsed": false, |
1787 | | - "input": [ |
1788 | | - "mb_class.index.is_unique" |
1789 | | - ], |
1790 | | - "language": "python", |
1791 | | - "metadata": {}, |
1792 | | - "outputs": [] |
1793 | | - }, |
1794 | 1822 | { |
1795 | 1823 | "cell_type": "markdown", |
1796 | 1824 | "metadata": {}, |
|
1802 | 1830 | "cell_type": "code", |
1803 | 1831 | "collapsed": false, |
1804 | 1832 | "input": [ |
1805 | | - "mb_class.groupby(level=0).sum().tail(10)" |
| 1833 | + "mb_class.groupby(level=0).sum().head(10)" |
1806 | 1834 | ], |
1807 | 1835 | "language": "python", |
1808 | 1836 | "metadata": {}, |
|
0 commit comments