DataFrame API Reference
Comprehensive PySpark DataFrame transformations, column projections, joins, and filtering operations.
Projects a set of expressions or column objects and returns a new DataFrame.
Used In: ETL pipelines, Column Pruning, Data Normalization
df.select(*cols)df.select('name', col('salary'), (col('salary') * 0.1).alias('bonus'))| name | salary | bonus |
|---|---|---|
| Alice | 90000 | 9000.0 |
| Bob | 75000 | 7500.0 |
Related Methods: selectExpr, withColumn
Projects SQL expressions specified as raw SQL string strings.
Used In: Complex SQL Expressions, Inline Type Casting
df.selectExpr(*exprs)df.selectExpr('name', 'salary * 1.1 as new_salary', 'CASE WHEN age >= 18 THEN "Adult" ELSE "Minor" END as category')| name | new_salary | category |
|---|---|---|
| Alice | 99000.0 | Adult |
Filters rows using a SQL condition string or PySpark Column boolean expression.
Used In: Data Cleansing, Partition Pruning, Outlier Removal
df.filter(condition) # Alias: df.where(condition)df.filter((col('age') >= 21) & (col('department') == 'Engineering'))| id | age | department |
|---|---|---|
| 10 | 28 | Engineering |
Returns a new DataFrame by adding a new column or replacing an existing column with the same name.
Used In: Feature Engineering, Column Transformation
df.withColumn(colName, colVal)df.withColumn('annual_income', col('monthly_income') * 12)| monthly_income | annual_income |
|---|---|
| 5000 | 60000 |
Renames an existing column in the DataFrame.
Used In: Schema Standardization, Data Normalization
df.withColumnRenamed(existing, new)df.withColumnRenamed('emp_id', 'employee_id')| employee_id | name |
|---|---|
| 101 | Alice |
Returns a new DataFrame with specified column(s) removed.
Used In: Payload Reduction, PII Removal
df.drop(*cols)df.drop('temp_col', 'unused_status')Schema without 'temp_col' and 'unused_status'Removes duplicate rows from the DataFrame, optionally considering only a subset of columns.
Used In: Deduplication, Primary Key Enforcement
df.dropDuplicates(subset=None) # distinct() checks all columnsdf.dropDuplicates(subset=['email'])Unique rows per email addressJoins with another DataFrame using the specified join key and strategy.
Used In: Table Merging, Relational Data Lookups
df1.join(df2, on=..., how='inner')df1.join(df2, df1.user_id == df2.user_id, how='left')Merged DataFrame containing combined columns