Feature Engineering mit PySpark
John Hogue
Lead Data Scientist, General Mills
| ROOF |
|---|
| Asphalt Shingles, Pitched, Age 8 Years or Less |
| Asphalt Shingles, Age Over 8 Years |
| Asphalt Shingles, Age 8 Years or Less |
| Asphalt Shingles |
| Roof_Age | wird zu | Roof>8yrs |
|---|---|---|
| Age 8 Years or Less | ⇨ | 0 |
| Age Over 8 Years | ⇨ | 1 |
| Age 8 Years or Less | ⇨ | 0 |
| NULL | ⇨ | NULL |
from pyspark.sql.functions import when# Boolesche Filter erstellen find_under_8 = df['ROOF'].like('%Age 8 Years or Less%') find_over_8 = df['ROOF'].like('%Age Over 8 Years%')# Filter mit when() und otherwise() anwenden df = df.withColumn('old_roof', (when(find_over_8, 1) .when(find_under_8, 0) .otherwise(None)))# Ergebnisse prüfen df[['ROOF', 'old_roof']].show(3, truncate=100)
+----------------------------------------------+--------+
| ROOF|old_roof|
+----------------------------------------------+--------+
| null| null|
|Asphalt Shingles, Pitched, Age 8 Years or Less| 0|
| Asphalt Shingles, Age Over 8 Years| 1|
+----------------------------------------------+--------+
only showing top 3 rows
| ROOF | wird zu | Roof_Material |
|---|---|---|
| Asphalt Shingles, Pitched, Age 8 Years or Less | ⇨ | Asphalt Shingles |
| Null | ⇨ | |
| Asphalt Shingles, Age Over 8 Years | ⇨ | Asphalt Shingles |
| Metal, Age 8 Years or Less | ⇨ | Metal |
| Tile, Age 8 Years or Less | ⇨ | Tile |
| Asphalt Shingles | ⇨ | Asphalt Shingles |
from pyspark.sql.functions import split# Spalte an Kommas in eine Liste aufteilen split_col = split(df['ROOF'], ',')# Ersten Listenwert in eine neue Spalte schreiben df = df.withColumn('Roof_Material', split_col.getItem(0))# Ergebnisse prüfen df[['ROOF', 'Roof_Material']].show(5, truncate=100)
+----------------------------------------------+----------------+
| ROOF| Roof_Material|
+----------------------------------------------+----------------+
| null| null|
|Asphalt Shingles, Pitched, Age 8 Years or Less|Asphalt Shingles|
| null| null|
|Asphalt Shingles, Pitched, Age 8 Years or Less|Asphalt Shingles|
| Asphalt Shingles, Age Over 8 Years|Asphalt Shingles|
+----------------------------------------------+----------------+
only showing top 5 rows
Ausgangsdatensatz
| NO | roof_list |
|---|---|
| 2 | [Asphalt Shingles, Pitched, Age 8 Years or Less] |
Explodierter Datensatz
| NO | ex_roof_list |
|---|---|
| 2 | Asphalt Shingles |
| 2 | Pitched |
| 2 | Age 8 Years or Less |
Explodierter Datensatz
| NO | ex_roof_list |
|---|---|
| 2 | Asphalt Shingles |
| 2 | Pitched |
| 2 | Age 8 Years or Less |
Gepivoteter Datensatz
| NO | Age 8 Years or Less | Age Over 8 Years | Asphalt Shingles | Flat | Metal | Other | Pitched | ... |
|---|---|---|---|---|---|---|---|---|
| 2 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | ... |
from pyspark.sql.functions import split, explode, lit, coalesce, first
# Spalte an Kommas in eine Liste aufteilen
df = df.withColumn('roof_list', split(df['ROOF'], ', '))
# Liste in neue Zeilen für jeden Wert explodieren
ex_df = df.withColumn('ex_roof_list', explode(df['roof_list']))
# Dummy-Spalte mit konstantem Wert erstellen
ex_df = ex_df.withColumn('constant_val', lit(1))
# Werte in boolesche Spalten pivotieren
piv_df = ex_df.groupBy('NO').pivot('ex_roof_list')\
.agg(coalesce(first('constant_val')))
Feature Engineering mit PySpark