熟悉pandas的pythoner 应该知道给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、 增加常数项
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、简单根据某列进行计算
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、定制化根据某列进行计算
比如我想对某列做指定操作,但是对应的函数没得咋办,造,自己造~
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|
+-----+---+---------+--------------------+-------------+
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]