溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pyspark如何給dataframe增加新的一列

發(fā)布時(shí)間:2020-07-31 09:29:27 來源:億速云 閱讀:522 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了pyspark如何給dataframe增加新的一列,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

熟悉pandas的pythoner 應(yīng)該知道給dataframe增加一列很容易,直接以字典形式指定就好了,pyspark中就不同了,摸索了一下,可以使用如下方式增加

from pyspark import SparkContext
from pyspark import SparkConf
from pypsark.sql import SparkSession
from pyspark.sql import functions

spark = SparkSession.builder.config(conf=SparkConf()).getOrCreate()

data =   [['Alice', 19, 'blue', '["Alice", 19, "blue"]'],
  ['Jane', 20, 'green', '["Jane", 20, "green"]'],
   ['Mary', 21, 'blue', '["Mary", 21, "blue"]'], ]
frame = spark.createDataFrame(data, schema=["name", "age", "eye_color", "detail"])

frame.cache()
frame.show()

+-----+---+---------+--------------------+
| name|age|eye_color|              detail|
+-----+---+---------+--------------------+
|Alice| 19|     blue|["Alice", 19, "bl...|
| Jane| 20|    green|["Jane", 20, "gre...|
| Mary| 21|     blue|["Mary", 21, "blue"]|
+-----+---+---------+--------------------+

1、 增加常數(shù)項(xiàng)

frame2 = frame.withColumn("contant", functions.lit(10))
frame2.show()

+-----+---+---------+--------------------+-------+
| name|age|eye_color|              detail|contant|
+-----+---+---------+--------------------+-------+
|Alice| 19|     blue|["Alice", 19, "bl...|     10|
| Jane| 20|    green|["Jane", 20, "gre...|     10|
| Mary| 21|     blue|["Mary", 21, "blue"]|     10|
+-----+---+---------+--------------------+-------+

2、簡(jiǎn)單根據(jù)某列進(jìn)行計(jì)算

2.1 使用 withColumn

frame3_1 = frame.withColumn("name_length", functions.length(frame.name))
frame3_1.show()

+-----+---+---------+--------------------+-----------+
| name|age|eye_color|              detail|name_length|
+-----+---+---------+--------------------+-----------+
|Alice| 19|     blue|["Alice", 19, "bl...|          5|
| Jane| 20|    green|["Jane", 20, "gre...|          4|
| Mary| 21|     blue|["Mary", 21, "blue"]|          4|
+-----+---+---------+--------------------+-----------+

2.2 使用 select

frame3_2 = frame.select(["name", functions.length(frame.name).alias("name_length")])
frame3_2.show()

+-----+-----------+
| name|name_length|
+-----+-----------+
|Alice|          5|
| Jane|          4|
| Mary|          4|
+-----+-----------+

2.3 使用 selectExpr

frame3_3 = frame.selectExpr(["name", "length(name) as name_length"])
frame3_3.show()

+-----+-----------+
| name|name_length|
+-----+-----------+
|Alice|          5|
| Jane|          4|
| Mary|          4|
+-----+-----------+

3、定制化根據(jù)某列進(jìn)行計(jì)算

比如我想對(duì)某列做指定操作,但是對(duì)應(yīng)的函數(shù)沒得咋辦,造,自己造~

frame4 = frame.withColumn("detail_length", functions.UserDefinedFunction(lambda obj: len(json.loads(obj)))(frame.detail))

# or
def length_detail(obj):
 return len(json.loads(obj))
frame4 = frame.withColumn("detail_length", functions.UserDefinedFunction(length_detail)(frame.detail))
frame4.show()

+-----+---+---------+--------------------+-------------+
| name|age|eye_color|              detail|detail_length|
+-----+---+---------+--------------------+-------------+
|Alice| 19|     blue|["Alice", 19, "bl...|            3|
| Jane| 20|    green|["Jane", 20, "gre...|            3|
| Mary| 21|     blue|["Mary", 21, "blue"]|            3|
+-----+---+---------+--------------------+-------------+

看完上述內(nèi)容,是不是對(duì)pyspark如何給dataframe增加新的一列有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI