Pyspark数据框将功能应用于行并将行添加到数据框的底部

时间:2020-06-02 17:21:04

标签: python pyspark

我有一个只有一行的df。

id   |id2  |score|score2|
----------------------
0    |1    |4    |2     |

,我想在底部添加一行百分比,即每个数字除以7

0/7  |1/7  |4/7  |2/7   |

但是我想出的解决方案非常慢

temp = [i/7 for i in df.collect()[0]]
row = sc.parallelize(Row(temp)).toDF()
df.union(row)

这花费了21秒钟来运行,几乎所有代码都是最后两行代码。有一个更好的方法吗?我的另一个想法是转置表格,然后可以使用df.withColumn()轻松完成。理想情况下,我也想用0过滤掉该列,但是我还没有真正研究过

1 个答案:

答案 0 :(得分:1)

检查一下,让我知道是否有帮助

 from pyspark.sql import SparkSession
 from pyspark.sql import functions as F

 spark = SparkSession.builder \
.appName('practice')\
.getOrCreate()

 sc= spark.sparkContext

 df = sc.parallelize([
(0,1,4,2)]).toDF(["id", "id2","score","score2"])


df2 = df.select(*[(F.col(column)/7).alias(column) for column in df.columns])

df3 = df.union(df2)

df3.show()
+---+-------------------+------------------+------------------+
| id|                id2|             score|            score2|
+---+-------------------+------------------+------------------+
|0.0|                1.0|               4.0|               2.0|
|0.0|0.14285714285714285|0.5714285714285714|0.2857142857142857|
+---+-------------------+------------------+------------------+

如果要。过滤出包含0的列,您可以使用以下代码

non_zero_cols  = [c for c in df.columns if df[[c]].first()[c] > 0]

df1 = df.select(*non_zero_cols)

df2 = df1.select(*[(F.col(column)/7).alias(column) for column in 
df1.columns])

df3 = df1.union(df2)

df3.show()

+-------------------+------------------+------------------+
|                id2|             score|            score2|
+-------------------+------------------+------------------+
|                1.0|               4.0|               2.0|
|0.14285714285714285|0.5714285714285714|0.2857142857142857|
+-------------------+------------------+------------------+

请检查以下具有类型列的df代码

non_zero_cols  = [c for c in df.columns if df[[c]].first()[c] > 0]

df1 = df.select(*non_zero_cols, F.lit('count').alias('type') )

df2 = df1.select(*[(F.col(column)/7).alias(column) for column in 
df1.columns if not column=='type'], F.lit('percent').alias('type'))

df3 = df1.union(df2)

df3.show()

+-------------------+------------------+------------------+-------+
|                id2|             score|            score2|   type|
+-------------------+------------------+------------------+-------+
|                1.0|               4.0|               2.0|  count|
|0.14285714285714285|0.5714285714285714|0.2857142857142857|percent|
+-------------------+------------------+------------------+-------+