熟悉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|
+-----+---+---------+--------------------+-------------+
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]