Încărcarea mai multor tabele cu joinuri

Ingestie eficientă a datelor cu pandas

Amany Mahfouz

Instructor

Chei

  • Înregistrările din baze de date au identificatori unici, sau chei

Date apeluri 311, cu coloana unique_key evidențiată. Cheile unice sunt numere.

Ingestie eficientă a datelor cu pandas

Chei

  • Înregistrările din baze de date au identificatori unici, sau chei

Date catalog cursuri, cu coloana course_code evidențiată. Codurile conțin abrevieri de discipline și numere.

Ingestie eficientă a datelor cu pandas

Chei

  • Înregistrările din baze de date au identificatori unici, sau chei

Date catalog cursuri, cu coloana instructor_id evidențiată. Valorile sunt numere întregi de 9 cifre.

Ingestie eficientă a datelor cu pandas

Chei

Tabelul profesori, cu coloana id evidențiată. Valorile sunt numere întregi de 9 cifre, ca în instructor_id din catalogul cursurilor.

Ingestie eficientă a datelor cu pandas

Chei

Date catalog cursuri cu coloana de nume a profesorilor adăugată prin join

Ingestie eficientă a datelor cu pandas

Unirea tabelelor

Date meteo, cu coloana date evidențiată

Date apeluri 311, cu coloana created_date evidențiată

Ingestie eficientă a datelor cu pandas

Unirea tabelelor

SELECT *
  FROM hpd311calls
Ingestie eficientă a datelor cu pandas

Unirea tabelelor

SELECT *
  FROM hpd311calls
       JOIN weather 
       ON hpd311calls.created_date = weather.date;
  • Utilizați notația cu punct (tabel.coloană) când lucrați cu mai multe tabele
  • Joinul implicit returnează doar înregistrările ale căror chei apar în ambele tabele
  • Asigurați-vă că cheile de join au același tip de date, altfel nu se va găsi nicio potrivire
Ingestie eficientă a datelor cu pandas

Unire și filtrare

/* Get only heat/hot water calls and join in weather data */
SELECT *
  FROM hpd311calls
       JOIN weather 
       ON hpd311calls.created_date = weather.date
 WHERE hpd311calls.complaint_type = 'HEAT/HOT WATER';
Ingestie eficientă a datelor cu pandas

Unire și agregare

/* Get call counts by borough */
SELECT hpd311calls.borough, 
         COUNT(*)
  FROM hpd311calls
 GROUP BY hpd311calls.borough;
Ingestie eficientă a datelor cu pandas

Unire și agregare

/* Get call counts by borough
   and join in population and housing counts */
SELECT hpd311calls.borough, 
       COUNT(*), 
       boro_census.total_population,
       boro_census.housing_units
  FROM hpd311calls
 GROUP BY hpd311calls.borough
Ingestie eficientă a datelor cu pandas

Unire și agregare

/* Get call counts by borough
   and join in population and housing counts */
SELECT hpd311calls.borough, 
       COUNT(*), 
       boro_census.total_population,
       boro_census.housing_units
  FROM hpd311calls
       JOIN boro_census 
       ON hpd311calls.borough = boro_census.borough
 GROUP BY hpd311calls.borough;
Ingestie eficientă a datelor cu pandas
query = """SELECT hpd311calls.borough, 
                    COUNT(*), 
                    boro_census.total_population,
                    boro_census.housing_units
             FROM hpd311calls
                  JOIN boro_census 
                  ON hpd311calls.borough = boro_census.borough
            GROUP BY hpd311calls.borough;"""

call_counts = pd.read_sql(query, engine)
print(call_counts)
         borough  COUNT(*)  total_population  housing_units
0          BRONX     29874           1455846         524488
1       BROOKLYN     31722           2635121        1028383
2      MANHATTAN     20196           1653877         872645
3         QUEENS     11384           2339280         850422
4  STATEN ISLAND      1322            475948         179179
Ingestie eficientă a datelor cu pandas

Recapitulare

  • Ordinea cuvintelor cheie SQL
    • SELECT
    • FROM
    • JOIN
    • WHERE
    • GROUP BY
Ingestie eficientă a datelor cu pandas

Să exersăm!

Ingestie eficientă a datelor cu pandas

Preparing Video For Download...