1
我是Spark新手,需要從單行生成多行和多列。如何在pyspark中從單行添加多行和多列?
輸入:
col1 col2 col3 col4
輸出
col1 col2 col3 col4 col5 col6 col7
col1 col2 col3 col4 col8 col9 col10
Logics for new columns:
**col5 :**
if col1==0 and col3!=0:
col5 = col4/col3
else:
col5 = 0
**col6 :**
if col1==0 and col4!=0:
col6 = (col3*col4)/col1
else:
col6 = 0
For first row col7 holds same value as col2
**col8 :**
if col1!=0 and col3!=0:
col8 = col4/col3
else:
col8 = 0
**col9 :**
if col1!=0 and col4!=0:
col9 = (col3*col4)/col1
else:
col9 = 0
For second row col10 = col2+ "_NEW"
在結束 '和' 功能需要由與組施加。一旦我們轉換上述結構,希望這會很容易。
google中的大多數文章解釋瞭如何使用「withcolumn」選項而不是多列來將單列添加到現有數據框。文章沒有解釋這種情況。所以我想請求你的幫助。
它完美地工作。謝謝 !如果可能的話,你能否給我建議一些學習材料,如書籍/論壇或在pyspark中熟悉的想法。 – user3150024
很高興它幫助!最好的地方是參考['Programming Guides'section](http://spark.apache.org/docs/latest/index.html)。 (順便說一句 - 如果你喜歡這個答案,那麼你應該[把它投票/標記爲正確答案]) – Prem
相同的查詢在pyspark shell中執行得更快,但CLI 。我的源數據以ORC格式存在於Hive中。任何建議請 – user3150024