2016-07-22 15 views
4

Ich bin total in einer verdrahteten Situation verloren. Jetzt habe ich eine Liste liErstellen Sie einen Datenrahmen aus einer Liste in pyspark.sql

li = example_data.map(lambda x: get_labeled_prediction(w,x)).collect() 
print li, type(li) 

der Ausgang ist wie,

[(0.0, 59.0), (0.0, 51.0), (0.0, 81.0), (0.0, 8.0), (0.0, 86.0), (0.0, 86.0), (0.0, 60.0), (0.0, 54.0), (0.0, 54.0), (0.0, 84.0)] <type 'list'> 

Wenn ich versuche, einen Datenrahmen aus dieser Liste

m = sqlContext.createDataFrame(l, ["prediction", "label"]) 

die Nachricht Fehler

warf zu erstellen
TypeError         Traceback (most recent call last) 
<ipython-input-90-4a49f7f67700> in <module>() 
56 l = example_data.map(lambda x: get_labeled_prediction(w,x)).collect() 
57 print l, type(l) 
---> 58 m = sqlContext.createDataFrame(l, ["prediction", "label"]) 
59 ''' 
60 g = example_data.map(lambda x:gradient_summand(w, x)).sum() 

/databricks/spark/python/pyspark/sql/context.py in createDataFrame(self, data, schema, samplingRatio) 
423    rdd, schema = self._createFromRDD(data, schema, samplingRatio) 
424   else: 
--> 425    rdd, schema = self._createFromLocal(data, schema) 
426   jrdd = self._jvm.SerDeUtil.toJavaArray(rdd._to_java_object_rdd()) 
427   jdf = self._ssql_ctx.applySchemaToPythonRDD(jrdd.rdd(), schema.json()) 

/databricks/spark/python/pyspark/sql/context.py in _createFromLocal(self, data, schema) 
339 
340   if schema is None or isinstance(schema, (list, tuple)): 
--> 341    struct = self._inferSchemaFromList(data) 
342    if isinstance(schema, (list, tuple)): 
343     for i, name in enumerate(schema): 

/databricks/spark/python/pyspark/sql/context.py in _inferSchemaFromList(self, data) 
239    warnings.warn("inferring schema from dict is deprecated," 
240       "please use pyspark.sql.Row instead") 
--> 241   schema = reduce(_merge_type, map(_infer_schema, data)) 
242   if _has_nulltype(schema): 
243    raise ValueError("Some of types cannot be determined after inferring") 

/databricks/spark/python/pyspark/sql/types.py in _infer_schema(row) 
831   raise TypeError("Can not infer schema for type: %s" % type(row)) 
832 
--> 833  fields = [StructField(k, _infer_type(v), True) for k, v in items] 
834  return StructType(fields) 
835 

/databricks/spark/python/pyspark/sql/types.py in _infer_type(obj) 
808    return _infer_schema(obj) 
809   except TypeError: 
--> 810    raise TypeError("not supported type: %s" % type(obj)) 
811 
812 

TypeError: not supported type: <type 'numpy.float64'> 

Aber wenn ich diese Liste hart in Zeile

tt = sqlContext.createDataFrame([(0.0, 59.0), (0.0, 51.0), (0.0, 81.0), (0.0, 8.0), (0.0, 86.0), (0.0, 86.0), (0.0, 60.0), (0.0, 54.0), (0.0, 54.0), (0.0, 84.0)], ["prediction", "label"]) 
tt.collect() 
kodieren

Es funktioniert gut.

[Row(prediction=0.0, label=59.0), 
Row(prediction=0.0, label=51.0), 
Row(prediction=0.0, label=81.0), 
Row(prediction=0.0, label=8.0), 
Row(prediction=0.0, label=86.0), 
Row(prediction=0.0, label=86.0), 
Row(prediction=0.0, label=60.0), 
Row(prediction=0.0, label=54.0), 
Row(prediction=0.0, label=54.0), 
Row(prediction=0.0, label=84.0)] 

was dieses Problem verursacht und wie es zu beheben? Jeder Hinweis wird geschätzt.

Antwort

4

Sie haben eine list of float64 und ich denke, dass es diesen Typ nicht mag. Auf der anderen Seite, wenn Sie es hart codieren, ist es nur ein list of float.
Hier ist eine question mit einer Antwort, die darüber hinausgeht, wie man von Numpy's Datentyp in native Python konvertiert.

+0

Danke, Limbo. Es ist genau das, wonach ich suche. –

+0

Ich folgte Ihrer vorgeschlagenen Antwort, aber es funktionierte nicht für mich. Ich bekomme 'TypeError: nicht unterstützt Typ: ' –