我有一些單獨的資料值,我必須將其轉換為資料框。我嘗試了以下。只有一行輸出會來。
val matchingcount= 3
val notmatchingcount=5
val filename=h:/filename1
import spark.implicits._
val data=Seq(" filename "," matchingcount "," notmatchingcount ").toDF("ezfilename","match_count","non_matchcount")
data.show()
throwing error :
Exception in thread "main" java.lang.IllegalArguementException : requirement failed : the number of columns doesn't match.
Old column names (1): value
New column names (8) : ezfilename,match_count,non_matchcount
請幫忙
uj5u.com熱心網友回復:
你快到了!執行您想要的操作的代碼如下:
val matchingcount= 3
val notmatchingcount=5
val filename="h:/filename1"
import spark.implicits._
val data=Seq((filename,matchingcount,notmatchingcount)).toDF("ezfilename","match_count","non_matchcount")
data.show()
------------ ----------- --------------
| ezfilename|match_count|non_matchcount|
------------ ----------- --------------
|h:/filename1| 3| 5|
------------ ----------- --------------
您的代碼與上面的代碼之間有 3 個主要區別:
- 在 Scala 中,字串必須被
"字符包圍。所以我將這些字符添加到val filename= - 您是正確的,因為您可以在 imports 之后使用 a
Seq來使用該方法,但是字串的每個元素都代表資料幀的一行。因此,您不是創建一個包含 3 列的資料框,而是創建一個包含 1 個元素的資料框。創建 3 列的方法是在. 因此,請注意后者之間的區別以及后者是正確的地方。您還可以通過執行以下操作創建多行:toDFspark.implicits._SeqSeq(bla,bla,bla)Seq((bla, bla, bla))Seq((bla, bli, blu), (blo, ble, bly)) - 在 Scala 中,訪問變數值的方式是簡單地寫變數名。所以寫
filename而不是" filename "是這樣做的正確方法。
希望這可以幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/536739.html
標籤:斯卡拉阿帕奇火花
上一篇:將函式傳遞給Scala中的另一個函式,得到“擴展函式Scala缺少引數型別”
下一篇:我有一個List[(List[A],List[R])]-分別由物件A和R的兩個串列組成的元組串列。如何為每個串列[A]“展平”串列[R]?
