Xi *_*ang 53 size shape dataframe pyspark
我试图找出PySpark中DataFrame的大小/形状.我没有看到一个可以做到这一点的功能.
在Python中,我可以做到
data.shape()
Run Code Online (Sandbox Code Playgroud)
PySpark中是否有类似的功能.这是我目前的解决方案,但我正在寻找一个元素
row_number = data.count()
column_number = len(data.dtypes)
Run Code Online (Sandbox Code Playgroud)
列数的计算并不理想......
Geo*_*her 68
print((df.count(), len(df.columns)))
Run Code Online (Sandbox Code Playgroud)
将此添加到您的代码中:
def spark_shape(self):
return (self.count(), len(self.columns))
pyspark.sql.dataframe.DataFrame.shape = spark_shape
Run Code Online (Sandbox Code Playgroud)
那你可以做
>>> df.shape()
(10000, 10)
Run Code Online (Sandbox Code Playgroud)
但请注意,.count()对于非常大的数据集,这可能会非常慢。
小智 9
print((df.count(), len(df.columns)))
Run Code Online (Sandbox Code Playgroud)
对于较小的数据集更容易。
但是,如果数据集很大,另一种方法是使用熊猫和箭头将数据框转换为熊猫 df 并调用 shape
spark.conf.set("spark.sql.execution.arrow.enabled", "true")
spark.conf.set("spark.sql.crossJoin.enabled", "true")
print(df.toPandas().shape)
Run Code Online (Sandbox Code Playgroud)