Skip to content

Commit 7567dc1

Browse files
committed
update Pandas value_counts()
1 parent 459b5ee commit 7567dc1

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed

data-analysis/046-pandas-value_counts/pandas-value_counts.ipynb

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 9,
18+
"execution_count": 1,
1919
"metadata": {},
2020
"outputs": [],
2121
"source": [
@@ -25,7 +25,7 @@
2525
},
2626
{
2727
"cell_type": "code",
28-
"execution_count": 10,
28+
"execution_count": 2,
2929
"metadata": {},
3030
"outputs": [
3131
{
@@ -166,7 +166,7 @@
166166
"4 0 373450 8.0500 NaN S "
167167
]
168168
},
169-
"execution_count": 10,
169+
"execution_count": 2,
170170
"metadata": {},
171171
"output_type": "execute_result"
172172
}
@@ -358,7 +358,11 @@
358358
"metadata": {},
359359
"outputs": [],
360360
"source": [
361-
"pd.set_option('display.float_format', '{:.2f}%'.format)"
361+
"pd.set_option('display.float_format', '{:.2%}'.format)\n",
362+
"\n",
363+
"# Note: By calling the statement, you will update Pandas default display settings \n",
364+
"# and apply to all float values. To reset it, you can call\n",
365+
"# pd.reset_option('display.float_format')"
362366
]
363367
},
364368
{
@@ -369,9 +373,9 @@
369373
{
370374
"data": {
371375
"text/plain": [
372-
"S 0.72%\n",
373-
"C 0.19%\n",
374-
"Q 0.09%\n",
376+
"S 72.44%\n",
377+
"C 18.90%\n",
378+
"Q 8.66%\n",
375379
"Name: Embarked, dtype: float64"
376380
]
377381
},
@@ -384,6 +388,56 @@
384388
"df['Embarked'].value_counts(normalize = True)"
385389
]
386390
},
391+
{
392+
"cell_type": "code",
393+
"execution_count": 11,
394+
"metadata": {},
395+
"outputs": [],
396+
"source": [
397+
"pd.reset_option('display.float_format')"
398+
]
399+
},
400+
{
401+
"cell_type": "code",
402+
"execution_count": 12,
403+
"metadata": {},
404+
"outputs": [
405+
{
406+
"data": {
407+
"text/html": [
408+
"<style type=\"text/css\" >\n",
409+
"</style><table id=\"T_de79ef6a_0f55_11ec_8a56_acde48001122\" ><thead> <tr> <th class=\"blank level0\" ></th> <th class=\"col_heading level0 col0\" >Embarked</th> </tr></thead><tbody>\n",
410+
" <tr>\n",
411+
" <th id=\"T_de79ef6a_0f55_11ec_8a56_acde48001122level0_row0\" class=\"row_heading level0 row0\" >S</th>\n",
412+
" <td id=\"T_de79ef6a_0f55_11ec_8a56_acde48001122row0_col0\" class=\"data row0 col0\" >72.44%</td>\n",
413+
" </tr>\n",
414+
" <tr>\n",
415+
" <th id=\"T_de79ef6a_0f55_11ec_8a56_acde48001122level0_row1\" class=\"row_heading level0 row1\" >C</th>\n",
416+
" <td id=\"T_de79ef6a_0f55_11ec_8a56_acde48001122row1_col0\" class=\"data row1 col0\" >18.90%</td>\n",
417+
" </tr>\n",
418+
" <tr>\n",
419+
" <th id=\"T_de79ef6a_0f55_11ec_8a56_acde48001122level0_row2\" class=\"row_heading level0 row2\" >Q</th>\n",
420+
" <td id=\"T_de79ef6a_0f55_11ec_8a56_acde48001122row2_col0\" class=\"data row2 col0\" >8.66%</td>\n",
421+
" </tr>\n",
422+
" </tbody></table>"
423+
],
424+
"text/plain": [
425+
"<pandas.io.formats.style.Styler at 0x7fb2914f74d0>"
426+
]
427+
},
428+
"execution_count": 12,
429+
"metadata": {},
430+
"output_type": "execute_result"
431+
}
432+
],
433+
"source": [
434+
"# Thanks for David B Rosen's advice (https://dabruro.medium.com/)\n",
435+
"#\n",
436+
"# Instead of Pandas display option, which would change the display of all float values, \n",
437+
"# you can can simply do this: \n",
438+
"df['Embarked'].value_counts(normalize = True).to_frame().style.format('{:.2%}')"
439+
]
440+
},
387441
{
388442
"cell_type": "markdown",
389443
"metadata": {},
@@ -393,7 +447,7 @@
393447
},
394448
{
395449
"cell_type": "code",
396-
"execution_count": 7,
450+
"execution_count": 13,
397451
"metadata": {},
398452
"outputs": [
399453
{
@@ -405,7 +459,7 @@
405459
"Name: Fare, dtype: int64"
406460
]
407461
},
408-
"execution_count": 7,
462+
"execution_count": 13,
409463
"metadata": {},
410464
"output_type": "execute_result"
411465
}
@@ -416,7 +470,7 @@
416470
},
417471
{
418472
"cell_type": "code",
419-
"execution_count": 8,
473+
"execution_count": 14,
420474
"metadata": {},
421475
"outputs": [
422476
{
@@ -428,7 +482,7 @@
428482
"Name: Fare, dtype: int64"
429483
]
430484
},
431-
"execution_count": 8,
485+
"execution_count": 14,
432486
"metadata": {},
433487
"output_type": "execute_result"
434488
}
@@ -446,7 +500,7 @@
446500
},
447501
{
448502
"cell_type": "code",
449-
"execution_count": 21,
503+
"execution_count": 15,
450504
"metadata": {},
451505
"outputs": [
452506
{
@@ -462,7 +516,7 @@
462516
"Name: Sex, dtype: int64"
463517
]
464518
},
465-
"execution_count": 21,
519+
"execution_count": 15,
466520
"metadata": {},
467521
"output_type": "execute_result"
468522
}
@@ -480,7 +534,7 @@
480534
},
481535
{
482536
"cell_type": "code",
483-
"execution_count": 5,
537+
"execution_count": 16,
484538
"metadata": {},
485539
"outputs": [
486540
{
@@ -556,7 +610,7 @@
556610
" female 203"
557611
]
558612
},
559-
"execution_count": 5,
613+
"execution_count": 16,
560614
"metadata": {},
561615
"output_type": "execute_result"
562616
}
@@ -574,7 +628,7 @@
574628
},
575629
{
576630
"cell_type": "code",
577-
"execution_count": 6,
631+
"execution_count": 17,
578632
"metadata": {},
579633
"outputs": [
580634
{
@@ -635,7 +689,7 @@
635689
"ant 6 0"
636690
]
637691
},
638-
"execution_count": 6,
692+
"execution_count": 17,
639693
"metadata": {},
640694
"output_type": "execute_result"
641695
}
@@ -651,7 +705,7 @@
651705
},
652706
{
653707
"cell_type": "code",
654-
"execution_count": 7,
708+
"execution_count": 18,
655709
"metadata": {},
656710
"outputs": [
657711
{
@@ -664,7 +718,7 @@
664718
"dtype: int64"
665719
]
666720
},
667-
"execution_count": 7,
721+
"execution_count": 18,
668722
"metadata": {},
669723
"output_type": "execute_result"
670724
}
@@ -675,7 +729,7 @@
675729
},
676730
{
677731
"cell_type": "code",
678-
"execution_count": 8,
732+
"execution_count": 19,
679733
"metadata": {},
680734
"outputs": [
681735
{
@@ -736,7 +790,7 @@
736790
"2 2 1"
737791
]
738792
},
739-
"execution_count": 8,
793+
"execution_count": 19,
740794
"metadata": {},
741795
"output_type": "execute_result"
742796
}

0 commit comments

Comments
 (0)