簡體   English   中英

Pyspark DataframeType error a: DoubleType can not accept object 'a' in type<class 'str'></class>

[英]Pyspark DataframeType error a: DoubleType can not accept object 'a' in type <class 'str'>

我有這個 function

customSchema = StructType([ \
    StructField("a", Doubletype(), True), \
    StructField("b", Doubletype(), True),
    StructField("c", Doubletype(), True), 
    StructField("d", Doubletype(), True)])


n_1= sc.textFile("/path/*.txt")\
        .mapPartitions(lambda partition: csv.reader([line.replace('\0','') for line in partition],delimiter=';', quotechar='"')).filter(lambda line: len(line) > 1 )\
        .toDF(customSchema)

這將創建一個 Dataframe,問題是 '.mapPartitions' 將用作默認類型 <class 'str'> 我需要在將其轉換為 Dataframe 之前將其轉換為 DoubleType。 任何想法?

樣本數據

[['0,01', '344,01', '0,00', '0,00']]

或者只是與

n_1= sc.textFile("/path/*.txt")\
        .mapPartitions(lambda partition: csv.reader([line.replace('\0','') for line in partition],delimiter=';', quotechar='"')).filter(lambda line: len(line) > 1 )\
       

首先,需要收集所有元素並使用第二個選項創建一個矩陣(列表列表)。

n_1= sc.textFile("/path/*.txt")\
        .mapPartitions(lambda partition: csv.reader([line.replace('\0','') for line in partition],delimiter=';', quotechar='"')).filter(lambda line: len(line) > 1 )\
 

matrix  = n_1.collect()

一旦我們有了這個,就必須知道哪種類型的數據進入子列表(在我的例子中是'str')。

matrix  =[[x.replace(',', '.') for x in i] for i in matrix ] # replace ',' for '.' in order to perform the data type convertion

matrix  = [[float(str(x)) for x in i] for i in matrix  ] #convert every sublist element into float

df = sc.parallelize(matrix).toDF()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM