PySpark के साथ Feature Engineering
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 | बनता है | 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# बूलियन फिल्टर बनाएँ find_under_8 = df['ROOF'].like('%Age 8 Years or Less%') find_over_8 = df['ROOF'].like('%Age Over 8 Years%')# when() और otherwise() का उपयोग कर फिल्टर लगाएँ df = df.withColumn('old_roof', (when(find_over_8, 1) .when(find_under_8, 0) .otherwise(None)))# परिणाम देखें 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 | बनता है | 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# कॉलम को कॉमा पर बाँटकर लिस्ट बनाएँ split_col = split(df['ROOF'], ',')# लिस्ट के पहले मान को नए कॉलम में रखें df = df.withColumn('Roof_Material', split_col.getItem(0))# परिणाम देखें 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
शुरुआती रिकॉर्ड
| NO | roof_list |
|---|---|
| 2 | [Asphalt Shingles, Pitched, Age 8 Years or Less] |
Exploded रिकॉर्ड
| NO | ex_roof_list |
|---|---|
| 2 | Asphalt Shingles |
| 2 | Pitched |
| 2 | Age 8 Years or Less |
Exploded रिकॉर्ड
| NO | ex_roof_list |
|---|---|
| 2 | Asphalt Shingles |
| 2 | Pitched |
| 2 | Age 8 Years or Less |
Pivoted रिकॉर्ड
| 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
# कॉलम को कॉमा पर बाँटकर लिस्ट बनाएँ
df = df.withColumn('roof_list', split(df['ROOF'], ', '))
# लिस्ट को explode कर प्रत्येक मान के लिए नए रिकॉर्ड बनाएँ
ex_df = df.withColumn('ex_roof_list', explode(df['roof_list']))
# एक स्थिर मान वाला डमी कॉलम बनाएँ
ex_df = ex_df.withColumn('constant_val', lit(1))
# मानों को पिवट कर बूलियन कॉलम बनाएँ
piv_df = ex_df.groupBy('NO').pivot('ex_roof_list')\
.agg(coalesce(first('constant_val')))
PySpark के साथ Feature Engineering