Interagindo com DataFrames do PySpark

Fundamentos de Big Data com PySpark

Upendra Devisetty

Science Analyst, CyVerse

Operadores de DataFrame no PySpark

  • Operações de DataFrame: Transformations e Actions

  • Transformations em DataFrame:

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

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

    Correção: printSchema() é um método para qualquer dataset/dataframe do Spark e não uma action

Fundamentos de Big Data com PySpark

Operações select() e show()

  • A transformação select() seleciona colunas do DataFrame
df_id_age = test.select('Age')
  • A action show() imprime as primeiras 20 linhas do DataFrame
df_id_age.show(3)
+---+
|Age|
+---+
| 17|
| 17|
| 17|
+---+
only showing top 3 rows
Fundamentos de Big Data com PySpark

Operações filter() e show()

  • A transformação filter() filtra linhas com base em uma condição
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
Fundamentos de Big Data com PySpark

Operações groupby() e count()

  • A operação groupby() agrupa por uma variável
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
Fundamentos de Big Data com PySpark

Transformações orderby()

  • A operação orderby() ordena o DataFrame por uma ou mais colunas
test_df_age_group.count().orderBy('Age').show(3)
+---+-----+
|Age|count|
+---+-----+
|  0|15098|
| 17|    4|
| 18|99660|
+---+-----+
only showing top 3 rows
Fundamentos de Big Data com PySpark

dropDuplicates()

  • dropDuplicates() remove as linhas duplicadas de um DataFrame
test_df_no_dup = test_df.select('User_ID','Gender', 'Age').dropDuplicates()
test_df_no_dup.count()
5892
Fundamentos de Big Data com PySpark

Transformações com withColumnRenamed

  • withColumnRenamed() renomeia uma coluna no 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|
+-------+---+---+
Fundamentos de Big Data com PySpark

printSchema()

  • A operação printSchema() exibe os tipos das colunas do 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)
Fundamentos de Big Data com PySpark

Ações columns

  • O operador columns lista as colunas de um DataFrame
test_df.columns
['User_ID', 'Gender', 'Age']
Fundamentos de Big Data com PySpark

Ações describe()

  • A operação describe() calcula estatísticas-resumo das colunas numéricas do 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|
+-------+------------------+------+------------------+
Fundamentos de Big Data com PySpark

Vamos praticar!

Fundamentos de Big Data com PySpark

Preparing Video For Download...