Skip to content

Commit fce9bb2

Browse files
committed
count and percentage
1 parent e827b23 commit fce9bb2

File tree

1 file changed

+328
-0
lines changed

1 file changed

+328
-0
lines changed
Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Pandas count and percentage by value for a column\n",
8+
"\n",
9+
"* read remote data from pdf\n",
10+
"* calculate count and percent\n",
11+
"* format percent in better output\n",
12+
"\n",
13+
"Bonus\n",
14+
"\n",
15+
"* pandas column renaming"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": 1,
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"data": {
25+
"text/html": [
26+
"<div>\n",
27+
"<style scoped>\n",
28+
" .dataframe tbody tr th:only-of-type {\n",
29+
" vertical-align: middle;\n",
30+
" }\n",
31+
"\n",
32+
" .dataframe tbody tr th {\n",
33+
" vertical-align: top;\n",
34+
" }\n",
35+
"\n",
36+
" .dataframe thead th {\n",
37+
" text-align: right;\n",
38+
" }\n",
39+
"</style>\n",
40+
"<table border=\"1\" class=\"dataframe\">\n",
41+
" <thead>\n",
42+
" <tr style=\"text-align: right;\">\n",
43+
" <th></th>\n",
44+
" <th>food</th>\n",
45+
" <th>Portion size</th>\n",
46+
" <th>per 100 grams</th>\n",
47+
" <th>energy</th>\n",
48+
" </tr>\n",
49+
" </thead>\n",
50+
" <tbody>\n",
51+
" <tr>\n",
52+
" <th>0</th>\n",
53+
" <td>Fish cake</td>\n",
54+
" <td>90 cals per cake</td>\n",
55+
" <td>200 cals</td>\n",
56+
" <td>Medium</td>\n",
57+
" </tr>\n",
58+
" <tr>\n",
59+
" <th>1</th>\n",
60+
" <td>Fish fingers</td>\n",
61+
" <td>50 cals per piece</td>\n",
62+
" <td>220 cals</td>\n",
63+
" <td>Medium</td>\n",
64+
" </tr>\n",
65+
" <tr>\n",
66+
" <th>2</th>\n",
67+
" <td>Gammon</td>\n",
68+
" <td>320 cals</td>\n",
69+
" <td>280 cals</td>\n",
70+
" <td>Med-High</td>\n",
71+
" </tr>\n",
72+
" <tr>\n",
73+
" <th>3</th>\n",
74+
" <td>Haddock fresh</td>\n",
75+
" <td>200 cals</td>\n",
76+
" <td>110 cals</td>\n",
77+
" <td>Low calorie</td>\n",
78+
" </tr>\n",
79+
" <tr>\n",
80+
" <th>4</th>\n",
81+
" <td>Halibut fresh</td>\n",
82+
" <td>220 cals</td>\n",
83+
" <td>125 cals</td>\n",
84+
" <td>Low calorie</td>\n",
85+
" </tr>\n",
86+
" </tbody>\n",
87+
"</table>\n",
88+
"</div>"
89+
],
90+
"text/plain": [
91+
" food Portion size per 100 grams energy\n",
92+
"0 Fish cake 90 cals per cake 200 cals Medium\n",
93+
"1 Fish fingers 50 cals per piece 220 cals Medium\n",
94+
"2 Gammon 320 cals 280 cals Med-High\n",
95+
"3 Haddock fresh 200 cals 110 cals Low calorie\n",
96+
"4 Halibut fresh 220 cals 125 cals Low calorie"
97+
]
98+
},
99+
"execution_count": 1,
100+
"metadata": {},
101+
"output_type": "execute_result"
102+
}
103+
],
104+
"source": [
105+
"from tabula import read_pdf\n",
106+
"import pandas as pd\n",
107+
"df = read_pdf(\"http://www.uncledavesenterprise.com/file/health/Food%20Calories%20List.pdf\", pages=3, pandas_options={'header': None})\n",
108+
"df.columns = ['food', 'Portion size ', 'per 100 grams', 'energy']\n",
109+
"df.head()"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 2,
115+
"metadata": {},
116+
"outputs": [],
117+
"source": [
118+
"s = df.energy"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 3,
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"data": {
128+
"text/plain": [
129+
"Medium 14\n",
130+
"High 6\n",
131+
"Low calorie 4\n",
132+
"Med-High 4\n",
133+
"Low-Med 1\n",
134+
"Low- Med 1\n",
135+
"Name: energy, dtype: int64"
136+
]
137+
},
138+
"execution_count": 3,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
143+
"source": [
144+
"counts = s.value_counts()\n",
145+
"counts"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 4,
151+
"metadata": {},
152+
"outputs": [
153+
{
154+
"data": {
155+
"text/plain": [
156+
"Medium 0.466667\n",
157+
"High 0.200000\n",
158+
"Low calorie 0.133333\n",
159+
"Med-High 0.133333\n",
160+
"Low-Med 0.033333\n",
161+
"Low- Med 0.033333\n",
162+
"Name: energy, dtype: float64"
163+
]
164+
},
165+
"execution_count": 4,
166+
"metadata": {},
167+
"output_type": "execute_result"
168+
}
169+
],
170+
"source": [
171+
"percent = s.value_counts(normalize=True)\n",
172+
"percent"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 5,
178+
"metadata": {},
179+
"outputs": [
180+
{
181+
"data": {
182+
"text/plain": [
183+
"Medium 46.7%\n",
184+
"High 20.0%\n",
185+
"Low calorie 13.3%\n",
186+
"Med-High 13.3%\n",
187+
"Low-Med 3.3%\n",
188+
"Low- Med 3.3%\n",
189+
"Name: energy, dtype: object"
190+
]
191+
},
192+
"execution_count": 5,
193+
"metadata": {},
194+
"output_type": "execute_result"
195+
}
196+
],
197+
"source": [
198+
"percent100 = s.value_counts(normalize=True).mul(100).round(1).astype(str) + '%'\n",
199+
"percent100"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 6,
205+
"metadata": {},
206+
"outputs": [
207+
{
208+
"data": {
209+
"text/html": [
210+
"<div>\n",
211+
"<style scoped>\n",
212+
" .dataframe tbody tr th:only-of-type {\n",
213+
" vertical-align: middle;\n",
214+
" }\n",
215+
"\n",
216+
" .dataframe tbody tr th {\n",
217+
" vertical-align: top;\n",
218+
" }\n",
219+
"\n",
220+
" .dataframe thead th {\n",
221+
" text-align: right;\n",
222+
" }\n",
223+
"</style>\n",
224+
"<table border=\"1\" class=\"dataframe\">\n",
225+
" <thead>\n",
226+
" <tr style=\"text-align: right;\">\n",
227+
" <th></th>\n",
228+
" <th>counts</th>\n",
229+
" <th>per</th>\n",
230+
" <th>per100</th>\n",
231+
" </tr>\n",
232+
" </thead>\n",
233+
" <tbody>\n",
234+
" <tr>\n",
235+
" <th>Medium</th>\n",
236+
" <td>14</td>\n",
237+
" <td>0.466667</td>\n",
238+
" <td>46.7%</td>\n",
239+
" </tr>\n",
240+
" <tr>\n",
241+
" <th>High</th>\n",
242+
" <td>6</td>\n",
243+
" <td>0.200000</td>\n",
244+
" <td>20.0%</td>\n",
245+
" </tr>\n",
246+
" <tr>\n",
247+
" <th>Low calorie</th>\n",
248+
" <td>4</td>\n",
249+
" <td>0.133333</td>\n",
250+
" <td>13.3%</td>\n",
251+
" </tr>\n",
252+
" <tr>\n",
253+
" <th>Med-High</th>\n",
254+
" <td>4</td>\n",
255+
" <td>0.133333</td>\n",
256+
" <td>13.3%</td>\n",
257+
" </tr>\n",
258+
" <tr>\n",
259+
" <th>Low-Med</th>\n",
260+
" <td>1</td>\n",
261+
" <td>0.033333</td>\n",
262+
" <td>3.3%</td>\n",
263+
" </tr>\n",
264+
" <tr>\n",
265+
" <th>Low- Med</th>\n",
266+
" <td>1</td>\n",
267+
" <td>0.033333</td>\n",
268+
" <td>3.3%</td>\n",
269+
" </tr>\n",
270+
" </tbody>\n",
271+
"</table>\n",
272+
"</div>"
273+
],
274+
"text/plain": [
275+
" counts per per100\n",
276+
"Medium 14 0.466667 46.7%\n",
277+
"High 6 0.200000 20.0%\n",
278+
"Low calorie 4 0.133333 13.3%\n",
279+
"Med-High 4 0.133333 13.3%\n",
280+
"Low-Med 1 0.033333 3.3%\n",
281+
"Low- Med 1 0.033333 3.3%"
282+
]
283+
},
284+
"execution_count": 6,
285+
"metadata": {},
286+
"output_type": "execute_result"
287+
}
288+
],
289+
"source": [
290+
"pd.DataFrame({'counts': counts, 'per': percent, 'per100': percent100})"
291+
]
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": null,
296+
"metadata": {},
297+
"outputs": [],
298+
"source": [
299+
"s = df.energy\n",
300+
"counts = s.value_counts()\n",
301+
"percent = s.value_counts(normalize=True)\n",
302+
"percent100 = s.value_counts(normalize=True).mul(100).round(1).astype(str) + '%'\n",
303+
"pd.DataFrame({'counts': counts, 'per': percent, 'per100': percent100})"
304+
]
305+
}
306+
],
307+
"metadata": {
308+
"kernelspec": {
309+
"display_name": "Python 3",
310+
"language": "python",
311+
"name": "python3"
312+
},
313+
"language_info": {
314+
"codemirror_mode": {
315+
"name": "ipython",
316+
"version": 3
317+
},
318+
"file_extension": ".py",
319+
"mimetype": "text/x-python",
320+
"name": "python",
321+
"nbconvert_exporter": "python",
322+
"pygments_lexer": "ipython3",
323+
"version": "3.6.7"
324+
}
325+
},
326+
"nbformat": 4,
327+
"nbformat_minor": 2
328+
}

0 commit comments

Comments
 (0)