Manipulace s daty pomocí DataFrames

Introduction to PySpark

Ben Schmidt

Data Engineer

Zpracování chybějících dat

  • Použijte .na.drop() k odstranění řádků s hodnotami null
# Drop rows with any nulls
df_cleaned = df.na.drop()

# Filter out nulls df_cleaned = df.where(col("columnName").isNotNull())
  • Použijte .na.fill({"column": value) k nahrazení hodnot null konkrétní hodnotou
# Fill nulls in the age column with the value 0
df_filled = df.na.fill({"age": 0})
Introduction to PySpark

Operace se sloupci

  • Použijte .withColumn() k přidání nového sloupce na základě výpočtů nebo existujících sloupců
# Create a new column 'age_plus_5'
df = df.withColumn("age_plus_5", df["age"] + 5)
  • Použijte withColumnRenamed() k přejmenování sloupců
# Rename the 'age' column to 'years'
df = df.withColumnRenamed("age", "years")
  • Použijte drop() k odstranění nepotřebných sloupců
# Drop the 'department' column
df = df.drop("department")
Introduction to PySpark

Operace s řádky

  • Použijte .filter() k výběru řádků podle zadaných podmínek
# Filter rows where salary is greater than 50000
filtered_df = df.filter(df["salary"] > 50000)
  • Použijte .groupBy() a agregační funkce (např. .sum(), .avg()) k sumarizaci dat
# Group by department and calculate the average salary
grouped_df = df.groupBy("department").avg("salary")
Introduction to PySpark

Výsledky operací s řádky

  • Filtrování

    +------+---+-----------------+
    |salary|age|      occupation |
    +------+---+-----------------+
    | 60000| 45|Exec-managerial  |
    | 70000| 35|Prof-specialty   |
    +------+---+-----------------+
    
  • GroupBy ` +----------+-----------+ |department|avg(salary)| +----------+-----------+ | HR| 80000.0| | IT| 70000.0| +----------+-----------+

`

Introduction to PySpark

Přehled syntaxe

# Drop rows with any nulls
df_cleaned = df.na.drop()

#Drop nulls on a column df_cleaned = df.where(col("columnName").isNotNull())
# Fill nulls in the age column with the value 0 df_filled = df.na.fill({"age": 0})
  • Použijte .withColumn() k přidání nového sloupce na základě výpočtů nebo existujících sloupců. Syntaxe: .withColumn("new_col_name", "original transformation")

    # Create a new column 'age_plus_5'
    df = df.withColumn("age_plus_5", df["age"] + 5)
    
  • Použijte withColumnRenamed() k přejmenování sloupců Syntaxe: withColumnRenamed(old column name,new column name`

# Rename the 'age' column to 'years'
df = df.withColumnRenamed("age", "years")
  • Použijte drop() k odstranění nepotřebných sloupců Syntaxe: .drop(column name)
# Drop the 'department' column
df = df.drop("department")
# Filter rows where salary is greater than 50000
filtered_df = df.filter(df["salary"] > 50000)
  • Použijte .groupBy() a agregační funkce (např. .sum(), .avg()) k sumarizaci dat
# Group by department and calculate the average salary
grouped_df = df.groupBy("department").avg("salary")
Introduction to PySpark

Pojďme si procvičit!

Introduction to PySpark

Preparing Video For Download...