2017-07-21 2 views
0

私はすべての値を、設定されたしきい値より低い値、つまりSpark 2.1.0、PySpark API、およびDataFrameを使用して置き換えようとしています。PySparkは、カウントしきい値より下の値を値で置き換えます。

例(df1以下)の関数をテストすると、機能します。しかし、私の実際のデータではありません。どちらもdtype属性文字列を持っています。 df1では、forループで 'cat'と 'integers'の列を実行します。これは、実際のデータDFでも必要なものです。

df1 = spark.createDataFrame([ 
    (0, "a","1"), 
    (1, "b","1"), 
    (2, "c","1"), 
    (3, "a","1"), 
    (4, "a","2"), 
    (5, "c","2"), 
    (6,"b","1"), 
    (7,"b","1"), 
], ["id", "cat","integer"]) 

def cutoff(df,feat,threshold, otherclass='other'): 
    if isinstance(threshold, float) and threshold<1: 
     threshold = str(int(threshold*df.count())) 

    if isinstance(threshold,int): 
     threshold = str(threshold) 

    temp = df.groupBy(feat).count().orderBy('count') 
    replace = temp.filter("count<"+threshold).select(feat).rdd.map(lambda r:r[0]).collect() 
    print "replacing ", replace,replace.__class__, " with ", otherclass, " subset ", feat 
    df = df.replace(replace,otherclass,feat) 

    return df 

しかし、(ハイブからSQLインポートを)実際のデータを使用しているとき、私は

mydata (just a part): 
+---------------+-----+ 
| browser_name|count| 
+---------------+-----+ 
|   Chrome| 2197| 
|    IE| 719| 
|  Firefox| 542| 
| Mobile Safari| 370| 
|Android Browser| 361| 
|   Edge| 265| 
| Chrome WebView| 203| 

replacing [u'Iron', u'UCBrowser', u'Puffin', u'Opera Mini', u'Yandex', u'Maxthon', u'Silk', u'Vivaldi', None, u'MIUI Browser', u'Chromium', u'WebKit', u'IEMobile', u'Facebook', u'Chrome WebView', u'Safari', u'Opera', u'Android Browser', u'Mobile Safari', u'Edge', u'IE', u'Firefox'] <type 'list'> with other subset browser_name 


Traceback (most recent call last): 
    File "/tmp/zeppelin_pyspark-1704248642413819893.py", line 267, in <module> 
    raise Exception(traceback.format_exc()) 
Exception: Traceback (most recent call last): 
    File "/tmp/zeppelin_pyspark-1704248642413819893.py", line 265, in <module> 
    exec(code) 
    File "<stdin>", line 12, in <module> 
    File "<stdin>", line 10, in cutoff 
    File "/usr/lib/spark/python/pyspark/sql/dataframe.py", line 1345, in replace 
    self._jdf.na().replace(self._jseq(subset), self._jmap(rep_dict)), self.sql_ctx) 
    File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__ 
    answer, self.gateway_client, self.target_id, self.name) 
    File "/usr/lib/spark/python/pyspark/sql/utils.py", line 63, in deco 
    return f(*a, **kw) 
    File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py", line 319, in get_return_value 
    format(target_id, ".", name), value) 
Py4JJavaError: An error occurred while calling o4471.replace. 
: scala.MatchError: null 
    at org.apache.spark.sql.DataFrameNaFunctions.replace0(DataFrameNaFunctions.scala:351) 
    at org.apache.spark.sql.DataFrameNaFunctions.replace(DataFrameNaFunctions.scala:336) 
    at sun.reflect.GeneratedMethodAccessor359.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244) 
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357) 
    at py4j.Gateway.invoke(Gateway.java:280) 
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) 
    at py4j.commands.CallCommand.execute(CallCommand.java:79) 
    at py4j.GatewayConnection.run(GatewayConnection.java:214) 
    at java.lang.Thread.run(Thread.java:745) 

を得るので、それはreplace機能について文句を言います。しかし、私が実行すると df.replace(['Chrome','Firefox'],'hovno','browser_name').show(10)は再びチャームのように機能します(関数がそれを生成し、そのokを出力するので、私はユニコードでリストを入力しようとしました)。だから私は何が私の仕事をしない機能で私のDFをやったのだろうか? MatchErrorは元の値を見つけることができないと分かっていますが、確かにそこにあります。

ありがとうございます!

答えて

0

問題は、私のデータフレームに、replaceが一致しないNone値が含まれていたことでした。回避策として、最初にfillnaを使用します。これは、None値を何かに置き換えてから、魅力的に機能します。

関連する問題