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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "\n",
    "current_year = datetime.now().year\n",
    "keep_alive = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read actors data\n",
    "df = pd.read_csv(\"data/name.basics.tsv\", sep=\"\\t\")\n",
    "df[\"birthYear\"] = pd.to_numeric(df[\"birthYear\"], errors=\"coerce\")\n",
    "df[\"deathYear\"] = pd.to_numeric(df[\"deathYear\"], errors=\"coerce\")\n",
    "\n",
    "# Prepare and cleanup actors data\n",
    "if keep_alive:\n",
    "    df = df[df[\"deathYear\"].isna()]\n",
    "\n",
    "# Drop rows with incomplete data\n",
    "df = df.dropna(subset=[\"primaryProfession\", \"birthYear\"])\n",
    "df = df[df.knownForTitles != \"\\\\N\"]\n",
    "\n",
    "# Get if a person is an actor or actress\n",
    "df[\"is_actor\"] = df.primaryProfession.apply(lambda x: \"actor\" in x.split(\",\"))\n",
    "df[\"is_actress\"] = df.primaryProfession.apply(lambda x: \"actress\" in x.split(\",\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A note on genders: I do not have data as to which gender an actor or actress identify as. It does not matter for this exercise in any case as we plan to look at facial feature irrespective of gender. I use the actor/actress information for two reasons:\n",
    "\n",
    "1. I only want to keep people who acted in a movie/show, not the rest of the production crew (which may or may not be a good idea in the first place)\n",
    "2. When doing the Bing Search, I realize that for some people that have homonyms in other professions (such as Graham Green), I need to add the word \"actor\" or \"actress\" to the search to get more reliable pictures. I initially only added *actor/actress* in the query which returned strange results in some cases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>nconst</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>is_actor</th>\n",
       "      <th>is_actress</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>False</th>\n",
       "      <th>True</th>\n",
       "      <td>1554197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">True</th>\n",
       "      <th>False</th>\n",
       "      <td>2537757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>True</th>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      nconst\n",
       "is_actor is_actress         \n",
       "False    True        1554197\n",
       "True     False       2537757\n",
       "         True            222"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby([\"is_actor\", \"is_actress\"]).count()[[\"nconst\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nconst</th>\n",
       "      <th>primaryName</th>\n",
       "      <th>birthYear</th>\n",
       "      <th>deathYear</th>\n",
       "      <th>primaryProfession</th>\n",
       "      <th>knownForTitles</th>\n",
       "      <th>is_actor</th>\n",
       "      <th>is_actress</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>98892</th>\n",
       "      <td>nm0103696</td>\n",
       "      <td>Moya Brady</td>\n",
       "      <td>1962.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actor,actress,soundtrack</td>\n",
       "      <td>tt0457513,tt1054606,tt0110647,tt0414387</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116253</th>\n",
       "      <td>nm0122062</td>\n",
       "      <td>Debbie David</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actor,actress,special_effects</td>\n",
       "      <td>tt0092455,tt0104743,tt0112178,tt0096875</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>301992</th>\n",
       "      <td>nm0318693</td>\n",
       "      <td>Kannu Gill</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actress,actor</td>\n",
       "      <td>tt0119721,tt0130197,tt0150992,tt0292490</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>830244</th>\n",
       "      <td>nm0881417</td>\n",
       "      <td>Mansi Upadhyay</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actress,actor</td>\n",
       "      <td>tt3815878,tt0374887,tt14412608,tt10719514</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>954524</th>\n",
       "      <td>nm10034909</td>\n",
       "      <td>Cheryl Kann</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actor,actress</td>\n",
       "      <td>tt8813608</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>968196</th>\n",
       "      <td>nm1004934</td>\n",
       "      <td>Niloufar Safaie</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actor,actress</td>\n",
       "      <td>tt0247638,tt1523296</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>975084</th>\n",
       "      <td>nm10056470</td>\n",
       "      <td>Lydia Barton</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actor,actress</td>\n",
       "      <td>\\N</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1235242</th>\n",
       "      <td>nm10334756</td>\n",
       "      <td>Chesca Foe-a-man</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>miscellaneous,actor,actress</td>\n",
       "      <td>tt9050468,tt5232792</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1353828</th>\n",
       "      <td>nm10460818</td>\n",
       "      <td>Bhumika Barot</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actress,actor</td>\n",
       "      <td>tt15102968,tt11569584,tt9747194,tt10795628</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1461875</th>\n",
       "      <td>nm10576223</td>\n",
       "      <td>Allison Orr</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>actor,actress</td>\n",
       "      <td>\\N</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             nconst       primaryName  birthYear  deathYear  \\\n",
       "98892     nm0103696        Moya Brady     1962.0        NaN   \n",
       "116253    nm0122062      Debbie David        NaN        NaN   \n",
       "301992    nm0318693        Kannu Gill        NaN        NaN   \n",
       "830244    nm0881417    Mansi Upadhyay        NaN        NaN   \n",
       "954524   nm10034909       Cheryl Kann        NaN        NaN   \n",
       "968196    nm1004934   Niloufar Safaie        NaN        NaN   \n",
       "975084   nm10056470      Lydia Barton        NaN        NaN   \n",
       "1235242  nm10334756  Chesca Foe-a-man        NaN        NaN   \n",
       "1353828  nm10460818     Bhumika Barot        NaN        NaN   \n",
       "1461875  nm10576223       Allison Orr        NaN        NaN   \n",
       "\n",
       "                     primaryProfession  \\\n",
       "98892         actor,actress,soundtrack   \n",
       "116253   actor,actress,special_effects   \n",
       "301992                   actress,actor   \n",
       "830244                   actress,actor   \n",
       "954524                   actor,actress   \n",
       "968196                   actor,actress   \n",
       "975084                   actor,actress   \n",
       "1235242    miscellaneous,actor,actress   \n",
       "1353828                  actress,actor   \n",
       "1461875                  actor,actress   \n",
       "\n",
       "                                     knownForTitles  is_actor  is_actress  \n",
       "98892       tt0457513,tt1054606,tt0110647,tt0414387      True        True  \n",
       "116253      tt0092455,tt0104743,tt0112178,tt0096875      True        True  \n",
       "301992      tt0119721,tt0130197,tt0150992,tt0292490      True        True  \n",
       "830244    tt3815878,tt0374887,tt14412608,tt10719514      True        True  \n",
       "954524                                    tt8813608      True        True  \n",
       "968196                          tt0247638,tt1523296      True        True  \n",
       "975084                                           \\N      True        True  \n",
       "1235242                         tt9050468,tt5232792      True        True  \n",
       "1353828  tt15102968,tt11569584,tt9747194,tt10795628      True        True  \n",
       "1461875                                          \\N      True        True  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.is_actor & df.is_actress].head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A few people are marked both as actor and actress in the IMDb data. Manually looking at these cases, it seems to be an error in the DB and they are actually actresses. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Keep only actors and actresses in the dataset\n",
    "# Assume that if someone is both marked as actor/actress, it's an actress\n",
    "df = df[df.is_actor | df.is_actress]\n",
    "\n",
    "df[\"role\"] = \"other\"\n",
    "df.loc[df.is_actor, \"role\"] = \"actor\"\n",
    "df.loc[df.is_actress, \"role\"] = \"actress\"  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nconst</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>role</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>actor</th>\n",
       "      <td>2537757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>actress</th>\n",
       "      <td>1554419</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          nconst\n",
       "role            \n",
       "actor    2537757\n",
       "actress  1554419"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"role\")[[\"nconst\"]].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get full list of movies/shows by actor\n",
    "dfat = pd.read_csv(\"data/title.principals.tsv.gz\", sep=\"\\t\")\n",
    "dfat = dfat[dfat.category.isin([\"actor\", \"actress\", \"self\"])][[\"tconst\", \"nconst\"]]\n",
    "\n",
    "# Get data for the movies/shows the actors appeared in\n",
    "dftr = pd.read_csv(\"data/title.ratings.tsv\", sep=\"\\t\")\n",
    "dftb = pd.read_csv(\"data/title.basics.tsv\", sep=\"\\t\")\n",
    "dftb[\"startYear\"] = pd.to_numeric(dftb[\"startYear\"], errors=\"coerce\")\n",
    "dftb[\"endYear\"] = pd.to_numeric(dftb[\"endYear\"], errors=\"coerce\")\n",
    "\n",
    "# Estimate last year the show/movie was released (TV shows span several years and might still be active)\n",
    "# This is used to later filter for actors that were recently acting in something\n",
    "dftb.loc[(dftb.titleType.isin([\"tvSeries\", \"tvMiniSeries\"]) & (dftb.endYear.isna())), \"lastYear\"] = current_year\n",
    "dftb[\"lastYear\"] = dftb[\"lastYear\"].fillna(dftb[\"startYear\"])\n",
    "dftb = dftb.dropna(subset=[\"lastYear\"])\n",
    "dftb = dftb[dftb.isAdult == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Aggregate stats for all movies the actor was known for\n",
    "dft = pd.merge(dftb, dftr, how=\"inner\", on=\"tconst\")\n",
    "del dftb, dftr\n",
    "dfat = pd.merge(dfat, dft, how=\"inner\", on=\"tconst\")\n",
    "del dft\n",
    "dfat[\"totalRating\"] = dfat.averageRating*dfat.numVotes\n",
    "dfat = dfat.groupby(\"nconst\").agg({\n",
    "    \"averageRating\": \"mean\", \n",
    "    \"totalRating\": \"sum\", \n",
    "    \"numVotes\": \"sum\", \n",
    "    \"tconst\": \"count\", \n",
    "    \"startYear\": \"min\", \n",
    "    \"lastYear\": \"max\"\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Merge everything with actor data and cleanup\n",
    "df = df.drop([\"deathYear\", \"knownForTitles\", \"primaryProfession\"], axis=1)\n",
    "df = pd.merge(df, dfat, how=\"inner\", on=\"nconst\").sort_values(\"totalRating\", ascending=False)\n",
    "df = df.dropna(subset=[\"birthYear\", \"startYear\", \"lastYear\"])\n",
    "df[[\"birthYear\", \"startYear\", \"lastYear\"]] = df[[\"birthYear\", \"startYear\", \"lastYear\"]].astype(int)\n",
    "df = df.round(2)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "90e1e830ac57dfc2c41e3e7a76c8ffd4bb6262b307f4273d56b17cf39c34bbe6"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 64-bit ('actor_matching': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}