2017-09-28 11 views
1

を使用Pyspark null( '')を文字列で置き換える方法が見つかりましたが、データフレームのすべてのセルが文字の間にこの文字列で埋められます。おそらく、システムは空でないセルの文字列の間にヌル( '')を見ます。Pyspark-値が空の文字列を入力する

これらは初期データフレームの値は以下のとおりです。これを使用した後

+-----------------+-----+ 
|CustomerRelStatus|count| 
+-----------------+-----+ 
| Ανοιχτος  | 477| 
| Κλειστος  | 68| 
| 'γνωστο   | 291| 
|     | 1165| 
+-----------------+-----+ 

newDf = df.withColumn('CustomerStatus', regexp_replace('CustomerRelStatus', '', '-1000')) 

それが返されます。

+--------------------+-----+ 
| CustomerRelStatus |count| 
+--------------------+-----+ 
|-1000Α-1000ν-1000...| 477| 
|-1000Κ-1000λ-1000...| 68| 
|-1000ʼ-1000γ-1000...| 291| 
|    -1000| 1165| 
+--------------------+-----+ 

は、他の方法はありますか?

答えて

0

は、この情報がお役に立てば幸い!

from pyspark.sql.functions import col, when 

#sample data 
df = sc.parallelize([['abc', '123'], 
        ['efg', '456'], 
        ['', '789']]).toDF(('CustomerRelStatus', 'count')) 

#replace empty string with 'null' and then impute missing value, OR directly impute it with '-1000' in 'otherwise' condition 
df = df.withColumn("CustomerStatus", 
        when(col('CustomerRelStatus') != '', col('CustomerRelStatus')).otherwise(None)).drop('CustomerRelStatus') 
df = df.na.fill({'CustomerStatus': '-1000'}) 
df.show() 

出力が

+-----+--------------+ 
|count|CustomerStatus| 
+-----+--------------+ 
| 123|   abc| 
| 456|   efg| 
| 789|   -1000| 
+-----+--------------+ 


それはあなたの問題を解決した場合はお知らせすることを忘れないでください:)

+0

あなたが(HTTPS [答えを受け入れる]する必要があり@VickyKです。あなたがあなたの問題を解決するのに役立つのであれば。 – Prem

0

私は多分これを試してみてください、あなたがregexp_replaceの第2引数にスペースが不足していると思う:

newDf = df.withColumn('CustomerStatus', regexp_replace('CustomerRelStatus', ' ', '-1000')) 
関連する問題