Skip to content

Commit bec5d67

Browse files
committed
Adapt for pandas 2.x
1 parent f29e5bd commit bec5d67

File tree

1 file changed

+102
-35
lines changed

1 file changed

+102
-35
lines changed

source-code/pandas/pivot_versus_pivot_table.ipynb

Lines changed: 102 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# pivot versus pivot_tabel"
7+
"# pivot versus pivot_table"
88
]
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": 13,
12+
"execution_count": 1,
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
@@ -24,6 +24,13 @@
2424
"pandas has two function to restructure dataframes. Although they are similar, each has its won applications."
2525
]
2626
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"## Data"
32+
]
33+
},
2734
{
2835
"cell_type": "markdown",
2936
"metadata": {},
@@ -33,7 +40,7 @@
3340
},
3441
{
3542
"cell_type": "code",
36-
"execution_count": 14,
43+
"execution_count": 2,
3744
"metadata": {},
3845
"outputs": [],
3946
"source": [
@@ -44,7 +51,7 @@
4451
},
4552
{
4653
"cell_type": "code",
47-
"execution_count": 15,
54+
"execution_count": 3,
4855
"metadata": {},
4956
"outputs": [],
5057
"source": [
@@ -62,7 +69,7 @@
6269
},
6370
{
6471
"cell_type": "code",
65-
"execution_count": 16,
72+
"execution_count": 4,
6673
"metadata": {},
6774
"outputs": [],
6875
"source": [
@@ -71,15 +78,15 @@
7178
},
7279
{
7380
"cell_type": "code",
74-
"execution_count": 17,
81+
"execution_count": 5,
7582
"metadata": {},
7683
"outputs": [
7784
{
7885
"name": "stdout",
7986
"output_type": "stream",
8087
"text": [
8188
"<class 'pandas.core.frame.DataFrame'>\n",
82-
"Int64Index: 62 entries, 0 to 61\n",
89+
"RangeIndex: 62 entries, 0 to 61\n",
8390
"Data columns (total 6 columns):\n",
8491
" # Column Non-Null Count Dtype \n",
8592
"--- ------ -------------- ----- \n",
@@ -90,7 +97,7 @@
9097
" 4 gender 55 non-null category \n",
9198
" 5 condition 55 non-null category \n",
9299
"dtypes: category(2), datetime64[ns](1), float32(2), int64(1)\n",
93-
"memory usage: 2.2 KB\n"
100+
"memory usage: 1.8 KB\n"
94101
]
95102
}
96103
],
@@ -100,7 +107,7 @@
100107
},
101108
{
102109
"cell_type": "code",
103-
"execution_count": 18,
110+
"execution_count": 6,
104111
"metadata": {},
105112
"outputs": [
106113
{
@@ -191,7 +198,7 @@
191198
"4 1 0.0 2012-10-02 14:00:00 37.500000 M A"
192199
]
193200
},
194-
"execution_count": 18,
201+
"execution_count": 6,
195202
"metadata": {},
196203
"output_type": "execute_result"
197204
}
@@ -216,7 +223,7 @@
216223
},
217224
{
218225
"cell_type": "code",
219-
"execution_count": 19,
226+
"execution_count": 7,
220227
"metadata": {},
221228
"outputs": [],
222229
"source": [
@@ -225,7 +232,7 @@
225232
},
226233
{
227234
"cell_type": "code",
228-
"execution_count": 20,
235+
"execution_count": 8,
229236
"metadata": {},
230237
"outputs": [
231238
{
@@ -274,7 +281,7 @@
274281
" 34 (condition, 8) 7 non-null category\n",
275282
" 35 (condition, 9) 7 non-null category\n",
276283
"dtypes: category(18), float32(18)\n",
277-
"memory usage: 1.7 KB\n"
284+
"memory usage: 1.9 KB\n"
278285
]
279286
}
280287
],
@@ -298,7 +305,7 @@
298305
},
299306
{
300307
"cell_type": "code",
301-
"execution_count": 24,
308+
"execution_count": 9,
302309
"metadata": {},
303310
"outputs": [],
304311
"source": [
@@ -308,7 +315,7 @@
308315
},
309316
{
310317
"cell_type": "code",
311-
"execution_count": 25,
318+
"execution_count": 10,
312319
"metadata": {},
313320
"outputs": [
314321
{
@@ -358,16 +365,16 @@
358365
"cell_type": "markdown",
359366
"metadata": {},
360367
"source": [
361-
"The `pivot_table` method on the other hand will only take the numerical columns into account."
368+
"The `pivot_table` method on the other hand will only take the numerical columns into account. Hence it will not work on this dataframe since it contains categorical data as well."
362369
]
363370
},
364371
{
365372
"cell_type": "code",
366-
"execution_count": 21,
373+
"execution_count": 11,
367374
"metadata": {},
368375
"outputs": [],
369376
"source": [
370-
"time_series_table = data.pivot_table(index='date', columns='patient')"
377+
"time_series_table = data.pivot_table(index='date', columns='patient', values=['dose', 'temperature'])"
371378
]
372379
},
373380
{
@@ -379,7 +386,7 @@
379386
},
380387
{
381388
"cell_type": "code",
382-
"execution_count": 22,
389+
"execution_count": 12,
383390
"metadata": {},
384391
"outputs": [
385392
{
@@ -427,20 +434,20 @@
427434
},
428435
{
429436
"cell_type": "code",
430-
"execution_count": 86,
437+
"execution_count": 13,
431438
"metadata": {},
432439
"outputs": [],
433440
"source": [
434441
"dose_table = data.pivot_table(index='date',\n",
435442
" values=['dose'],\n",
436443
" columns='patient',\n",
437-
" aggfunc=np.sum,\n",
444+
" aggfunc='sum',\n",
438445
" margins=True,)"
439446
]
440447
},
441448
{
442449
"cell_type": "code",
443-
"execution_count": 52,
450+
"execution_count": 14,
444451
"metadata": {},
445452
"outputs": [
446453
{
@@ -620,7 +627,7 @@
620627
"All 6.0 15.0 13.0 10.0 27.0 8.0 30.0 0.0 30.0 139.0"
621628
]
622629
},
623-
"execution_count": 52,
630+
"execution_count": 14,
624631
"metadata": {},
625632
"output_type": "execute_result"
626633
}
@@ -645,7 +652,7 @@
645652
},
646653
{
647654
"cell_type": "code",
648-
"execution_count": 73,
655+
"execution_count": 15,
649656
"metadata": {},
650657
"outputs": [
651658
{
@@ -710,15 +717,15 @@
710717
" B 40.700001"
711718
]
712719
},
713-
"execution_count": 73,
720+
"execution_count": 15,
714721
"metadata": {},
715722
"output_type": "execute_result"
716723
}
717724
],
718725
"source": [
719726
"data.pivot_table(index=['gender', 'condition'],\n",
720727
" values='temperature',\n",
721-
" aggfunc=np.max,)"
728+
" aggfunc='max',)"
722729
]
723730
},
724731
{
@@ -730,7 +737,7 @@
730737
},
731738
{
732739
"cell_type": "code",
733-
"execution_count": 76,
740+
"execution_count": 16,
734741
"metadata": {},
735742
"outputs": [
736743
{
@@ -767,43 +774,93 @@
767774
" </thead>\n",
768775
" <tbody>\n",
769776
" <tr>\n",
770-
" <th rowspan=\"3\" valign=\"top\">F</th>\n",
777+
" <th rowspan=\"9\" valign=\"top\">F</th>\n",
778+
" <th>1</th>\n",
779+
" <td>0.0</td>\n",
780+
" <td>NaN</td>\n",
781+
" </tr>\n",
782+
" <tr>\n",
771783
" <th>2</th>\n",
772784
" <td>15.0</td>\n",
773785
" <td>39.400002</td>\n",
774786
" </tr>\n",
775787
" <tr>\n",
788+
" <th>3</th>\n",
789+
" <td>0.0</td>\n",
790+
" <td>NaN</td>\n",
791+
" </tr>\n",
792+
" <tr>\n",
793+
" <th>4</th>\n",
794+
" <td>0.0</td>\n",
795+
" <td>NaN</td>\n",
796+
" </tr>\n",
797+
" <tr>\n",
798+
" <th>5</th>\n",
799+
" <td>0.0</td>\n",
800+
" <td>NaN</td>\n",
801+
" </tr>\n",
802+
" <tr>\n",
776803
" <th>6</th>\n",
777804
" <td>8.0</td>\n",
778805
" <td>38.099998</td>\n",
779806
" </tr>\n",
780807
" <tr>\n",
808+
" <th>7</th>\n",
809+
" <td>0.0</td>\n",
810+
" <td>NaN</td>\n",
811+
" </tr>\n",
812+
" <tr>\n",
781813
" <th>8</th>\n",
782814
" <td>0.0</td>\n",
783815
" <td>37.900002</td>\n",
784816
" </tr>\n",
785817
" <tr>\n",
786-
" <th rowspan=\"5\" valign=\"top\">M</th>\n",
818+
" <th>9</th>\n",
819+
" <td>0.0</td>\n",
820+
" <td>NaN</td>\n",
821+
" </tr>\n",
822+
" <tr>\n",
823+
" <th rowspan=\"9\" valign=\"top\">M</th>\n",
787824
" <th>1</th>\n",
788825
" <td>6.0</td>\n",
789826
" <td>38.500000</td>\n",
790827
" </tr>\n",
791828
" <tr>\n",
829+
" <th>2</th>\n",
830+
" <td>0.0</td>\n",
831+
" <td>NaN</td>\n",
832+
" </tr>\n",
833+
" <tr>\n",
792834
" <th>3</th>\n",
793835
" <td>13.0</td>\n",
794836
" <td>39.500000</td>\n",
795837
" </tr>\n",
796838
" <tr>\n",
839+
" <th>4</th>\n",
840+
" <td>0.0</td>\n",
841+
" <td>NaN</td>\n",
842+
" </tr>\n",
843+
" <tr>\n",
797844
" <th>5</th>\n",
798845
" <td>27.0</td>\n",
799846
" <td>39.500000</td>\n",
800847
" </tr>\n",
801848
" <tr>\n",
849+
" <th>6</th>\n",
850+
" <td>0.0</td>\n",
851+
" <td>NaN</td>\n",
852+
" </tr>\n",
853+
" <tr>\n",
802854
" <th>7</th>\n",
803855
" <td>30.0</td>\n",
804856
" <td>40.700001</td>\n",
805857
" </tr>\n",
806858
" <tr>\n",
859+
" <th>8</th>\n",
860+
" <td>0.0</td>\n",
861+
" <td>NaN</td>\n",
862+
" </tr>\n",
863+
" <tr>\n",
807864
" <th>9</th>\n",
808865
" <td>30.0</td>\n",
809866
" <td>40.200001</td>\n",
@@ -815,17 +872,27 @@
815872
"text/plain": [
816873
" dose temperature\n",
817874
"gender patient \n",
818-
"F 2 15.0 39.400002\n",
875+
"F 1 0.0 NaN\n",
876+
" 2 15.0 39.400002\n",
877+
" 3 0.0 NaN\n",
878+
" 4 0.0 NaN\n",
879+
" 5 0.0 NaN\n",
819880
" 6 8.0 38.099998\n",
881+
" 7 0.0 NaN\n",
820882
" 8 0.0 37.900002\n",
883+
" 9 0.0 NaN\n",
821884
"M 1 6.0 38.500000\n",
885+
" 2 0.0 NaN\n",
822886
" 3 13.0 39.500000\n",
887+
" 4 0.0 NaN\n",
823888
" 5 27.0 39.500000\n",
889+
" 6 0.0 NaN\n",
824890
" 7 30.0 40.700001\n",
891+
" 8 0.0 NaN\n",
825892
" 9 30.0 40.200001"
826893
]
827894
},
828-
"execution_count": 76,
895+
"execution_count": 16,
829896
"metadata": {},
830897
"output_type": "execute_result"
831898
}
@@ -834,15 +901,15 @@
834901
"data.pivot_table(index=['gender', 'patient'],\n",
835902
" values=['temperature', 'dose'],\n",
836903
" aggfunc={\n",
837-
" 'temperature': np.max,\n",
838-
" 'dose': np.sum,\n",
904+
" 'temperature': 'max',\n",
905+
" 'dose': 'sum',\n",
839906
" },)"
840907
]
841908
}
842909
],
843910
"metadata": {
844911
"kernelspec": {
845-
"display_name": "Python 3",
912+
"display_name": "Python 3 (ipykernel)",
846913
"language": "python",
847914
"name": "python3"
848915
},
@@ -856,7 +923,7 @@
856923
"name": "python",
857924
"nbconvert_exporter": "python",
858925
"pygments_lexer": "ipython3",
859-
"version": "3.7.6"
926+
"version": "3.12.0"
860927
}
861928
},
862929
"nbformat": 4,

0 commit comments

Comments
 (0)