Feature Engineering cu PySpark
John Hogue
Lead Data Scientist, General Mills

df.select(['NO', 'UNITNUMBER', 'CLASS']).show()
+----+----------+-----+
| NO|UNITNUMBER|CLASS|
+----+----------+-----+
| 1| null| SF|
| 156| A8| SF|
| 157| 207| SF|
| 158| 701G| SF|
| 159| 36| SF|
Mai multe câmpuri nu sunt necesare pentru analiză
'NO' număr de înregistrare generat automat'UNITNUMBER' date irelevante'CLASS' valoare constantă drop(*cols)
*cols – un nume de coloană sau o listă de nume de coloane de eliminat.# List of columns to drop cols_to_drop = ['NO', 'UNITNUMBER', 'CLASS']# Drop the columns df = df.drop(*cols_to_drop)
where(condition)types.BooleanType sau un șir de expresie SQL.like(other)~df = df.where(~df['POTENTIALSHORTSALE'].like('Not Disclosed'))
Filtrați datele la trei abateri standard (3σ) față de medie (μ)

# Calculate values used for filtering std_val = df.agg({'SALESCLOSEPRICE': 'stddev'}).collect()[0][0] mean_val = df.agg({'SALESCLOSEPRICE': 'mean'}).collect()[0][0]# Create three standard deviation (μ ± 3σ) upper and lower bounds for data hi_bound = mean_val + (3 * std_val) low_bound = mean_val - (3 * std_val)# Use where() to filter the DataFrame between values df = df.where((df['LISTPRICE'] < hi_bound) & (df['LISTPRICE'] > low_bound))
DataFrame.dropna()
how: 'any' sau 'all'. Cu 'any', se elimină înregistrarea dacă conține orice valoare nulă. Cu 'all', doar dacă toate valorile sunt nule.thresh: int, implicit None. Dacă este specificat, elimină înregistrările cu mai puțin de thresh valori nenule. Suprascrie parametrul how.subset: listă opțională de coloane de luat în considerare.# Drop any records with NULL values df = df.dropna()# drop records if both LISTPRICE and SALESCLOSEPRICE are NULL df = df.dropna(how='all', subset['LISTPRICE', 'SALESCLOSEPRICE '])# Drop records where at least two columns have NULL values df = df.dropna(thresh=2)
Ce este un duplicat?
dropDuplicates()
# Entire DataFrame df.dropDuplicates()# Check only a column list df.dropDuplicates(['streetaddress'])
Feature Engineering cu PySpark