LOC Colors - Data Management.ipynb 23.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LOC Colors - Data Management"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Export data as minimal JSON files - only the essentials to create the swatches in the browser*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To use the created swatches for [https://labs.onb.ac.at/en/topic/akon-swatches/](https://labs.onb.ac.at/en/topic/akon-swatches/), I have to create a JSON file looking like this:\n",
    "\n",
    "```json\n",
    "[[\"AK111_461\", [\"#f4e6cd\", \"#cac4b2\", \"#7e8077\", \"#3e4139\", \"#2f3431\", \"#000304\"], \"Nonza\", \"gelaufen 1903\"],\n",
    "[\"AK111_072\", [\"#e2d7c1\", \"#a19c8f\", \"#504e42\", \"#494a44\", \"#010500\", \"#393c39\"], \"Kirchberg am Walde\", \"gelaufen 1914\"],\n",
    "[\"AK111_077\", [\"#454234\", \"#3e3b1f\", \"#7f7e77\", \"#a9b8be\", \"#3b4347\", \"#425a6b\"], \"Kirchberg am Wechsel\", \"gelaufen 1913\"]]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first part (the id and the colors) are part of the created swatches, the second part (a name and an approximate date) are part of the metadata to download here: [https://labs.onb.ac.at/gitlab/labs-team/raw-metadata/raw/master/akon_postcards_public_domain.csv.bz2?inline=false](https://labs.onb.ac.at/gitlab/labs-team/raw-metadata/raw/master/akon_postcards_public_domain.csv.bz2?inline=false)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read created swatches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
csteindl's avatar
Binder    
csteindl committed
59
    "df = pd.read_csv('https://labs.onb.ac.at/gitlab/labs-team/color-swatches-data/-/raw/master/historic_postcards_color_swatches.csv.bz2?inline=false', compression='bz2')"
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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>Unnamed: 0</th>\n",
       "      <th>akon_id</th>\n",
       "      <th>image_link</th>\n",
       "      <th>hex_colors</th>\n",
       "      <th>html</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>21914</th>\n",
       "      <td>21914</td>\n",
       "      <td>AK036_405</td>\n",
       "      <td>https://iiif.onb.ac.at/images/AKON/AK036_405/4...</td>\n",
       "      <td>['#f2e3c1', '#e6dec6', '#8e8a7a', '#7b7864', '...</td>\n",
       "      <td>&lt;a href=\"https://iiif.onb.ac.at/images/AKON/AK...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Unnamed: 0    akon_id  \\\n",
       "21914       21914  AK036_405   \n",
       "\n",
       "                                              image_link  \\\n",
       "21914  https://iiif.onb.ac.at/images/AKON/AK036_405/4...   \n",
       "\n",
       "                                              hex_colors  \\\n",
       "21914  ['#f2e3c1', '#e6dec6', '#8e8a7a', '#7b7864', '...   \n",
       "\n",
       "                                                    html  \n",
       "21914  <a href=\"https://iiif.onb.ac.at/images/AKON/AK...  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sample()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Only keep the necessary columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "id_and_hex_colors = df[['akon_id', 'hex_colors']].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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>akon_id</th>\n",
       "      <th>hex_colors</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>14980</th>\n",
       "      <td>AK010_595</td>\n",
       "      <td>['#cfbfa4', '#62583b', '#c8c7b6', '#4f5144', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7474</th>\n",
       "      <td>AK084_243</td>\n",
       "      <td>['#aca693', '#414028', '#444537', '#5c5f57', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23730</th>\n",
       "      <td>AK043_578</td>\n",
       "      <td>['#4e502c', '#444735', '#51554a', '#dae1d1', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30352</th>\n",
       "      <td>AK085_096</td>\n",
       "      <td>['#b5aa9d', '#f3e7d7', '#756d62', '#211a0f', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22389</th>\n",
       "      <td>AK038_067</td>\n",
       "      <td>['#f2e9cb', '#989384', '#545245', '#fcf7db', '...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         akon_id                                         hex_colors\n",
       "14980  AK010_595  ['#cfbfa4', '#62583b', '#c8c7b6', '#4f5144', '...\n",
       "7474   AK084_243  ['#aca693', '#414028', '#444537', '#5c5f57', '...\n",
       "23730  AK043_578  ['#4e502c', '#444735', '#51554a', '#dae1d1', '...\n",
       "30352  AK085_096  ['#b5aa9d', '#f3e7d7', '#756d62', '#211a0f', '...\n",
       "22389  AK038_067  ['#f2e9cb', '#989384', '#545245', '#fcf7db', '..."
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_and_hex_colors.sample(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Parse Color Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to properly export the color array as a JSON array later, convert the data representation slightly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "id_and_hex_colors['colors'] = id_and_hex_colors['hex_colors'].apply(\n",
    "    lambda c: json.loads(c.replace(\"'\", '\"'))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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>akon_id</th>\n",
       "      <th>hex_colors</th>\n",
       "      <th>colors</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1291</th>\n",
       "      <td>AK116_455</td>\n",
       "      <td>['#f4ece0', '#d4d2ce', '#716d5c', '#65665e', '...</td>\n",
       "      <td>[#f4ece0, #d4d2ce, #716d5c, #65665e, #b2b4bb, ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        akon_id                                         hex_colors  \\\n",
       "1291  AK116_455  ['#f4ece0', '#d4d2ce', '#716d5c', '#65665e', '...   \n",
       "\n",
       "                                                 colors  \n",
       "1291  [#f4ece0, #d4d2ce, #716d5c, #65665e, #b2b4bb, ...  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_and_hex_colors.sample()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Do you see the subtle difference? The entry in the colors column is now an array with strings _without_ the single quotes `'`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "id_and_colors = id_and_hex_colors[['akon_id', 'colors']].copy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add Metadata From Original Records"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next up: Combining the id and the color array with names and dates. Read the metadata dump directly from the link found on [https://labs.onb.ac.at/en/dataset/akon/](https://labs.onb.ac.at/en/dataset/akon/):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2785: DtypeWarning: Columns (13) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  interactivity=interactivity, compiler=compiler, result=result)\n"
     ]
    }
   ],
   "source": [
    "original = pd.read_csv('https://labs.onb.ac.at/gitlab/labs-team/raw-metadata/raw/master/akon_postcards_public_domain.csv.bz2?inline=false', compression='bz2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "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>akon_id</th>\n",
       "      <th>name</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>16251</th>\n",
       "      <td>AK016_087</td>\n",
       "      <td>Neulengbach</td>\n",
       "      <td>vor 1907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23864</th>\n",
       "      <td>AK044_401</td>\n",
       "      <td>Gloriette</td>\n",
       "      <td>1907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33292</th>\n",
       "      <td>AK085_517</td>\n",
       "      <td>Milano</td>\n",
       "      <td>vor 1905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13875</th>\n",
       "      <td>AK008_070</td>\n",
       "      <td>Schloß Schönbrunn</td>\n",
       "      <td>1906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12223</th>\n",
       "      <td>AK004_040</td>\n",
       "      <td>Pürgg</td>\n",
       "      <td>1909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4129</th>\n",
       "      <td>AK125_097</td>\n",
       "      <td>Altaussee</td>\n",
       "      <td>gelaufen 1901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23756</th>\n",
       "      <td>AK044_080</td>\n",
       "      <td>Radstädter Tauern</td>\n",
       "      <td>1907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26479</th>\n",
       "      <td>AK069_067</td>\n",
       "      <td>Attersee</td>\n",
       "      <td>1906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14251</th>\n",
       "      <td>AK109_329</td>\n",
       "      <td>Josefsthal</td>\n",
       "      <td>vor 1907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28518</th>\n",
       "      <td>AK063_083</td>\n",
       "      <td>Gaußig</td>\n",
       "      <td>1908</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         akon_id               name           date\n",
       "16251  AK016_087        Neulengbach       vor 1907\n",
       "23864  AK044_401          Gloriette           1907\n",
       "33292  AK085_517             Milano       vor 1905\n",
       "13875  AK008_070  Schloß Schönbrunn           1906\n",
       "12223  AK004_040              Pürgg           1909\n",
       "4129   AK125_097          Altaussee  gelaufen 1901\n",
       "23756  AK044_080  Radstädter Tauern           1907\n",
       "26479  AK069_067           Attersee           1906\n",
       "14251  AK109_329         Josefsthal       vor 1907\n",
       "28518  AK063_083             Gaußig           1908"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "original[['akon_id', 'name', 'date']].sample(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These are the columns needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "original_info = original[['akon_id', 'name', 'date']].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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>akon_id</th>\n",
       "      <th>name</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>24871</th>\n",
       "      <td>AK048_377</td>\n",
       "      <td>Reichenau an der Rax</td>\n",
       "      <td>1925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4191</th>\n",
       "      <td>AK054_543</td>\n",
       "      <td>Aflenz Kurort</td>\n",
       "      <td>vor 1905</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         akon_id                  name      date\n",
       "24871  AK048_377  Reichenau an der Rax      1925\n",
       "4191   AK054_543         Aflenz Kurort  vor 1905"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "original_info.sample(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas offers a handy function for merging two dataframes _not on the index_, but on a shared column:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors_and_info = pd.merge(id_and_colors, original_info, on='akon_id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "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>akon_id</th>\n",
       "      <th>colors</th>\n",
       "      <th>name</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>16356</th>\n",
       "      <td>AK016_310</td>\n",
       "      <td>[#5f5a57, #9c938c, #4d453f, #cabbab, #3c3b39, ...</td>\n",
       "      <td>Kapellen</td>\n",
       "      <td>1908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1117</th>\n",
       "      <td>AK116_129</td>\n",
       "      <td>[#dcc9ab, #cec0a7, #a8a290, #48473a, #6c6c62, ...</td>\n",
       "      <td>Garsten</td>\n",
       "      <td>gelaufen 1902</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         akon_id                                             colors      name  \\\n",
       "16356  AK016_310  [#5f5a57, #9c938c, #4d453f, #cabbab, #3c3b39, ...  Kapellen   \n",
       "1117   AK116_129  [#dcc9ab, #cec0a7, #a8a290, #48473a, #6c6c62, ...   Garsten   \n",
       "\n",
       "                date  \n",
       "16356           1908  \n",
       "1117   gelaufen 1902  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colors_and_info.sample(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's exactly what's needed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save to JSON-File"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas can export the data exactly in the target format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors_and_info.to_json('historic_postcards__id_colors_name_date.json', orient='values')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extract Subset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's almost all. For the preview on [https://labs.onb.ac.at/en/dataset/akon/](https://labs.onb.ac.at/en/dataset/akon/) I need a subset of 100 swatches and save them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "id_and_colors_100 = colors_and_info.iloc[:100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "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>akon_id</th>\n",
       "      <th>colors</th>\n",
       "      <th>name</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>AK111_207</td>\n",
       "      <td>[#403e2c, #f9f8eb, #7e7f6b, #35362d, #32342f, ...</td>\n",
       "      <td>Klosterneuburg</td>\n",
       "      <td>gelaufen 1908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>AK111_293</td>\n",
       "      <td>[#e6d8bf, #c7c1b1, #4a4c3c, #aeafa5, #464740, ...</td>\n",
       "      <td>Komotau</td>\n",
       "      <td>vor 1905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>AK111_184</td>\n",
       "      <td>[#f7edd6, #d1cbb8, #f6f0db, #2d2a1b, #9e9f93, ...</td>\n",
       "      <td>Klausenburg</td>\n",
       "      <td>gelaufen 1904</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AK111_026</td>\n",
       "      <td>[#e2cba6, #9e8e73, #574c39, #3c311a, #4c473b, ...</td>\n",
       "      <td>Kierling</td>\n",
       "      <td>1922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>AK111_072</td>\n",
       "      <td>[#e5d9c2, #c0baac, #928e81, #4c493e, #484943, ...</td>\n",
       "      <td>Kirchberg am Walde</td>\n",
       "      <td>gelaufen 1914</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      akon_id                                             colors  \\\n",
       "99  AK111_207  [#403e2c, #f9f8eb, #7e7f6b, #35362d, #32342f, ...   \n",
       "34  AK111_293  [#e6d8bf, #c7c1b1, #4a4c3c, #aeafa5, #464740, ...   \n",
       "86  AK111_184  [#f7edd6, #d1cbb8, #f6f0db, #2d2a1b, #9e9f93, ...   \n",
       "3   AK111_026  [#e2cba6, #9e8e73, #574c39, #3c311a, #4c473b, ...   \n",
       "39  AK111_072  [#e5d9c2, #c0baac, #928e81, #4c493e, #484943, ...   \n",
       "\n",
       "                  name           date  \n",
       "99      Klosterneuburg  gelaufen 1908  \n",
       "34             Komotau       vor 1905  \n",
       "86         Klausenburg  gelaufen 1904  \n",
       "3             Kierling           1922  \n",
       "39  Kirchberg am Walde  gelaufen 1914  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_and_colors_100.sample(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "id_and_colors_100.to_json('historic_postcards__id_colors_name_date__100.json', orient='values')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And done!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 User Default",
   "language": "python",
   "name": "python_3_user_default"
  },
  "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
csteindl's avatar
Binder    
csteindl committed
838
}