Úvod do JSON

Streamlined Data Ingestion with pandas

Amany Mahfouz

Instructor

JavaScript Object Notation (JSON)

  • Běžný webový formát dat
  • Není tabulkový
    • Záznamy nemusí mít stejnou sadu atributů
  • Data jsou organizována do kolekcí objektů
  • Objekty jsou kolekce párů atribut–hodnota
  • Vnořený JSON: objekty uvnitř objektů
Streamlined Data Ingestion with pandas

Čtení dat JSON

  • read_json()
    • Přijímá cestu k souboru JSON nebo JSON data jako řetězec
    • Datové typy lze určit pomocí argumentu dtype
    • Argument orient pro nestandardní rozložení JSON dat
      • možné hodnoty v dokumentaci pandas
Streamlined Data Ingestion with pandas

Orientace dat

  • Data JSON nejsou tabulková
    • pandas odhaduje, jak je uspořádat do tabulky
  • pandas dokáže automaticky zpracovat běžné orientace
Streamlined Data Ingestion with pandas

Orientace podle záznamů

  • Nejběžnější uspořádání JSON
    [
      {
          "age_adjusted_death_rate": "7.6",
          "death_rate": "6.2",
          "deaths": "32",
          "leading_cause": "Accidents Except Drug Posioning (V01-X39, X43, X45-X59, Y85-Y86)",
          "race_ethnicity": "Asian and Pacific Islander",
          "sex": "F",
          "year": "2007"
      },
      {
          "age_adjusted_death_rate": "8.1",
          "death_rate": "8.3",
          "deaths": "87",
          ...
    
Streamlined Data Ingestion with pandas

Orientace podle sloupců

  • Prostorově úspornější než orientace podle záznamů
    {
      "age_adjusted_death_rate": {
          "0": "7.6",
          "1": "8.1",
          "2": "7.1",
          "3": ".",
          "4": ".",
          "5": "7.3",
          "6": "13",
          "7": "20.6",
          "8": "17.4",
          "9": ".",
          "10": ".",
          "11": "19.8",
          ...
    
Streamlined Data Ingestion with pandas

Určení orientace

  • Data s orientací split – nyc_death_causes.json
    {
      "columns": [
          "age_adjusted_death_rate",
          "death_rate",
          "deaths",
          "leading_cause",
          "race_ethnicity",
          "sex",
          "year"
      ],
      "index": [...],
      "data": [
          [
              "7.6",
    
Streamlined Data Ingestion with pandas

Určení orientace

import pandas as pd

death_causes = pd.read_json("nyc_death_causes.json", orient="split")
print(death_causes.head())
  age_adjusted_death_rate death_rate deaths             leading_cause              race_ethnicity sex  year
0                     7.6        6.2     32  Accidents Except Drug...  Asian and Pacific Islander   F  2007
1                     8.1        8.3     87  Accidents Except Drug...          Black Non-Hispanic   F  2007
2                     7.1        6.1     71  Accidents Except Drug...                    Hispanic   F  2007
3                       .          .      .  Accidents Except Drug...          Not Stated/Unknown   F  2007
4                       .          .      .  Accidents Except Drug...       Other Race/ Ethnicity   F  2007

[5 rows x 7 columns]
Streamlined Data Ingestion with pandas

Pojďme si procvičit!

Streamlined Data Ingestion with pandas

Preparing Video For Download...