Skip to content

Commit 3e50460

Browse files
author
Christopher Fonnesbeck
committed
Cleanup of section 2
1 parent 961fee9 commit 3e50460

1 file changed

Lines changed: 64 additions & 36 deletions

File tree

2. Data Wrangling with Pandas.ipynb

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@
509509
"Notice that `mmsi` field that was an index on the `vessels` table is no longer an index on the merged table."
510510
]
511511
},
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+
},
512529
{
513530
"cell_type": "markdown",
514531
"metadata": {},
@@ -588,8 +605,8 @@
588605
"cell_type": "code",
589606
"collapsed": false,
590607
"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",
593610
"mb1.shape, mb2.shape"
594611
],
595612
"language": "python",
@@ -606,6 +623,13 @@
606623
"metadata": {},
607624
"outputs": []
608625
},
626+
{
627+
"cell_type": "markdown",
628+
"metadata": {},
629+
"source": [
630+
"Let's give the index and columns meaningful labels:"
631+
]
632+
},
609633
{
610634
"cell_type": "code",
611635
"collapsed": false,
@@ -626,6 +650,16 @@
626650
"metadata": {},
627651
"outputs": []
628652
},
653+
{
654+
"cell_type": "code",
655+
"collapsed": false,
656+
"input": [
657+
"mb1.head()"
658+
],
659+
"language": "python",
660+
"metadata": {},
661+
"outputs": []
662+
},
629663
{
630664
"cell_type": "markdown",
631665
"metadata": {},
@@ -754,7 +788,7 @@
754788
"cell_type": "code",
755789
"collapsed": false,
756790
"input": [
757-
"mb1.combine_first(mb2).shape"
791+
"mb1.combine_first(mb2).head()"
758792
],
759793
"language": "python",
760794
"metadata": {},
@@ -788,7 +822,7 @@
788822
"cell_type": "code",
789823
"collapsed": false,
790824
"input": [
791-
"pd.concat({'patient1': mb1, 'patient2': mb2}, axis=1).head()"
825+
"pd.concat(dict(patient1=mb1, patient2=mb2), axis=1).head()"
792826
],
793827
"language": "python",
794828
"metadata": {},
@@ -909,18 +943,29 @@
909943
"metadata": {},
910944
"outputs": []
911945
},
946+
{
947+
"cell_type": "code",
948+
"collapsed": false,
949+
"input": [
950+
"cdystonia2.index.is_unique"
951+
],
952+
"language": "python",
953+
"metadata": {},
954+
"outputs": []
955+
},
912956
{
913957
"cell_type": "markdown",
914958
"metadata": {},
915959
"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`."
917961
]
918962
},
919963
{
920964
"cell_type": "code",
921965
"collapsed": false,
922966
"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()"
924969
],
925970
"language": "python",
926971
"metadata": {},
@@ -950,17 +995,7 @@
950995
"cell_type": "code",
951996
"collapsed": false,
952997
"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()"
964999
],
9651000
"language": "python",
9661001
"metadata": {},
@@ -970,7 +1005,7 @@
9701005
"cell_type": "markdown",
9711006
"metadata": {},
9721007
"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:"
9741009
]
9751010
},
9761011
{
@@ -1329,7 +1364,7 @@
13291364
"source": [
13301365
"### Permutation and sampling\n",
13311366
"\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."
13331368
]
13341369
},
13351370
{
@@ -1376,7 +1411,7 @@
13761411
"source": [
13771412
"## Exercise\n",
13781413
"\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."
13801415
]
13811416
},
13821417
{
@@ -1395,9 +1430,9 @@
13951430
"\n",
13961431
"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",
13971432
"\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"
14011436
]
14021437
},
14031438
{
@@ -1456,7 +1491,9 @@
14561491
"\n",
14571492
"For example, we may want to aggregate our data with with some function.\n",
14581493
"\n",
1459-
"![split-apply-combine](http://f.cl.ly/items/0s0Z252j0X0c3k3P1M47/Screen%20Shot%202013-06-02%20at%203.04.04%20PM.png)"
1494+
"![split-apply-combine](http://f.cl.ly/items/0s0Z252j0X0c3k3P1M47/Screen%20Shot%202013-06-02%20at%203.04.04%20PM.png)\n",
1495+
"\n",
1496+
"*(taken from \"Python for Data Analysis\")*"
14601497
]
14611498
},
14621499
{
@@ -1516,6 +1553,7 @@
15161553
"cell_type": "code",
15171554
"collapsed": false,
15181555
"input": [
1556+
"# The median of the `twstrs` variable\n",
15191557
"cdystonia_grouped['twstrs'].quantile(0.5)"
15201558
],
15211559
"language": "python",
@@ -1678,7 +1716,7 @@
16781716
"collapsed": false,
16791717
"input": [
16801718
"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]"
16821720
],
16831721
"language": "python",
16841722
"metadata": {},
@@ -1781,16 +1819,6 @@
17811819
"metadata": {},
17821820
"outputs": []
17831821
},
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-
},
17941822
{
17951823
"cell_type": "markdown",
17961824
"metadata": {},
@@ -1802,7 +1830,7 @@
18021830
"cell_type": "code",
18031831
"collapsed": false,
18041832
"input": [
1805-
"mb_class.groupby(level=0).sum().tail(10)"
1833+
"mb_class.groupby(level=0).sum().head(10)"
18061834
],
18071835
"language": "python",
18081836
"metadata": {},

0 commit comments

Comments
 (0)