Interazione con i DataFrame PySpark

Fondamenti di Big Data con PySpark

Upendra Devisetty

Science Analyst, CyVerse

Operatori DataFrame in PySpark

  • Operazioni sui DataFrame: Transformations e Actions

  • Transformations sui DataFrame:

    • select(), filter(), groupby(), orderby(), dropDuplicates() e withColumnRenamed()
  • Actions sui DataFrame:

    • printSchema(), head(), show(), count(), columns e describe()

    Correzione: printSchema() è un metodo per qualsiasi dataset/dataframe Spark e non un'action

Fondamenti di Big Data con PySpark

Operazioni select() e show()

  • La transformation select() seleziona un sottoinsieme di colonne del DataFrame
df_id_age = test.select('Age')
  • L'action show() stampa le prime 20 righe del DataFrame
df_id_age.show(3)
+---+
|Age|
+---+
| 17|
| 17|
| 17|
+---+
only showing top 3 rows
Fondamenti di Big Data con PySpark

Operazioni filter() e show()

  • La transformation filter() filtra le righe in base a una condizione
new_df_age21 = new_df.filter(new_df.Age > 21)
new_df_age21.show(3)
+-------+------+---+
|User_ID|Gender|Age|
+-------+------+---+
|1000002|     M| 55|
|1000003|     M| 26|
|1000004|     M| 46|
+-------+------+---+
only showing top 3 rows
Fondamenti di Big Data con PySpark

Operazioni groupby() e count()

  • L'operazione groupby() raggruppa una variabile
test_df_age_group = test_df.groupby('Age')
test_df_age_group.count().show(3)
+---+------+
|Age| count|
+---+------+
| 26|219587|
| 17|     4|
| 55| 21504|
+---+------+
only showing top 3 rows
Fondamenti di Big Data con PySpark

Transformations orderby()

  • L'operazione orderby() ordina il DataFrame in base a una o più colonne
test_df_age_group.count().orderBy('Age').show(3)
+---+-----+
|Age|count|
+---+-----+
|  0|15098|
| 17|    4|
| 18|99660|
+---+-----+
only showing top 3 rows
Fondamenti di Big Data con PySpark

dropDuplicates()

  • dropDuplicates() rimuove le righe duplicate di un DataFrame
test_df_no_dup = test_df.select('User_ID','Gender', 'Age').dropDuplicates()
test_df_no_dup.count()
5892
Fondamenti di Big Data con PySpark

Transformations withColumnRenamed

  • withColumnRenamed() rinomina una colonna del DataFrame
test_df_sex = test_df.withColumnRenamed('Gender', 'Sex')
test_df_sex.show(3)
+-------+---+---+
|User_ID|Sex|Age|
+-------+---+---+
|1000001|  F| 17|
|1000001|  F| 17|
|1000001|  F| 17|
+-------+---+---+
Fondamenti di Big Data con PySpark

printSchema()

  • L'operazione printSchema() stampa i tipi di colonna nel DataFrame
test_df.printSchema()
 |-- User_ID: integer (nullable = true)
 |-- Product_ID: string (nullable = true)
 |-- Gender: string (nullable = true)
 |-- Age: string (nullable = true)
 |-- Occupation: integer (nullable = true)
 |-- Purchase: integer (nullable = true)
Fondamenti di Big Data con PySpark

Azioni columns

  • L'operatore columns stampa le colonne di un DataFrame
test_df.columns
['User_ID', 'Gender', 'Age']
Fondamenti di Big Data con PySpark

Azioni describe()

  • L'operazione describe() calcola statistiche descrittive delle colonne numeriche del DataFrame
test_df.describe().show()
+-------+------------------+------+------------------+
|summary|           User_ID|Gender|               Age|
+-------+------------------+------+------------------+
|  count|            550068|550068|            550068|
|   mean|1003028.8424013031|  null|30.382052764385495|
| stddev|1727.5915855307312|  null|11.866105189533554|
|    min|           1000001|     F|                 0|
|    max|           1006040|     M|                55|
+-------+------------------+------+------------------+
Fondamenti di Big Data con PySpark

Esercitiamoci!

Fondamenti di Big Data con PySpark

Preparing Video For Download...