pyspark dataframe api速覽


快速了解dataframe 提供的功能. 避免重復工作

版本 spark 2.2

相關性
cov 皮爾遜相關系數
corr 方差

刪除
dropDuplicates 可指定列
dropna 可指定列

選擇
select
selectExpr 支持 sql 表達式的select
colRegex 正則表達式選擇列
where
filter
exceptAll 在df1不在df2
union 並 根據列index
unionByName 根據列名union
subtract 差
intersect 交, 不去重
intersectAll 去重
limit
first
head
take
randomSplit 比例切分
sample 采樣
sampleBy 根據列值采樣 只支持一列

排序
orderBy
sort 支持多種寫法 見附錄
sortWithinPartitions

存儲
cache
registerTempTable
coalesce 存儲分塊 對比!!
repartition 存儲分塊
repartitionByRange 根據給定表達式partition
write 支持多種mode, 自定義partition nums 也可以自己寫一個connector例如tfrecord connector
writeStream
toDF
toJSON
toPandas 注意excutor要裝過pandas 自己當做package傳上去可能會由於大小受限制
checkpoint 版本存儲
localCheckpoint
persist
unpersist

修改
withColumn
withColumnRenamed
replace 支持多值替換, 類型必須相同
fillna
na

展示/統計
show truncate每列展示值作截斷, vertical 垂直展示
schema
stat
dtypes
describe 對比
summary
printSchema
rollup 根據所有列組合groupby and agg
freqItems
foreach shorthand for df.rdd.foreach()
foreachPartition

聚合
groupby
agg

other
toLocalIterator
approxQuantile
explain
hint
isLocal
isStreaming
withWatermark

附錄
sort

df.sort(df.age.desc()).collect()
[Row(age=5, name='Bob'), Row(age=2, name='Alice')]

df.sort("age", ascending=False).collect()
[Row(age=5, name='Bob'), Row(age=2, name='Alice')]

df.orderBy(df.age.desc()).collect()
[Row(age=5, name='Bob'), Row(age=2, name='Alice')]

from pyspark.sql.functions import *
df.sort(asc("age")).collect()
[Row(age=2, name='Alice'), Row(age=5, name='Bob')]

df.orderBy(desc("age"), "name").collect()
[Row(age=5, name='Bob'), Row(age=2, name='Alice')]

df.orderBy(["age", "name"], ascending=[0, 1]).collect()
[Row(age=5, name='Bob'), Row(age=2, name='Alice')]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM