Kullanmak lit, sütunun tüm değerlerini verilen değere dönüştürür.
Bunu yalnızca veri çerçevesinin boş olmayan değerleri için yapmak için, her sütunun boş olmayan değerlerini filtrelemeniz ve değerinizi değiştirmeniz gerekir. whenbunu başarmanıza yardımcı olabilir.
from pyspark.sql.functions import when
df.withColumn('c1', when(df.c1.isNotNull(), 1))
.withColumn('c2', when(df.c2.isNotNull(), 1))
.withColumn('c3', when(df.c3.isNotNull(), 1))
Bunun sonucu:
123c111n u l lc21n u l l1c311n u l l
Eğer çok başka bir değer olan null değerleri değiştirmek istiyorsanız Ayrıca, kullanabilirsiniz otherwisebirlikte when. Diyelim ki 0oraya ima etmek istiyorsunuz :
from pyspark.sql.functions import when
df.withColumn('c1', when(df.c1.isNotNull(), 1).otherwise(0))
.withColumn('c2', when(df.c2.isNotNull(), 1).otherwise(0))
.withColumn('c3', when(df.c3.isNotNull(), 1).otherwise(0))
Bunun sonucu:
123c1110c2101c3110