Apache spark Pyspark:如何在Pyspark中聚合列表中所有元素的数据?
我将所有字符串字段存储在列表对象中。然后,目前我正在传递for循环中的每个字段以计算聚合计数 我正在寻找一种方法,一次获得所有字符串列的聚合计数。请帮忙 样本数据: Dataframe(输入数据)有这些记录Apache spark Pyspark:如何在Pyspark中聚合列表中所有元素的数据?,apache-spark,pyspark,apache-spark-sql,pyspark-sql,Apache Spark,Pyspark,Apache Spark Sql,Pyspark Sql,我将所有字符串字段存储在列表对象中。然后,目前我正在传递for循环中的每个字段以计算聚合计数 我正在寻找一种方法,一次获得所有字符串列的聚合计数。请帮忙 样本数据: Dataframe(输入数据)有这些记录 NoOfSegments,SegmentID,Country 3,2,Bangalore 3,2,Bangalore 3,3,Delhi 3,2,Delhi 3,3,Delhi 3,1,Pune 3,3,Bangalore 3,1,Pune 3,1,Delhi 3,3,Bangalore 3
NoOfSegments,SegmentID,Country
3,2,Bangalore
3,2,Bangalore
3,3,Delhi
3,2,Delhi
3,3,Delhi
3,1,Pune
3,3,Bangalore
3,1,Pune
3,1,Delhi
3,3,Bangalore
3,1,Delhi
3,3,Bangalore
3,3,Pune
3,2,Delhi
3,3,Pune
3,2,Pune
3,2,Pune
3,3,Pune
3,1,Bangalore
3,1,Bangalore
我的代码:
input_data.createOrReplaceTempView('input_data')
sub="string"
category_columns = [name for name, data_type in input_data.dtypes
if sub in data_type]
df_final_schema = StructType([StructField("Country", StringType())
, StructField("SegmentID", IntegerType())
, StructField("total_cnt", IntegerType())
])
df_final=spark.createDataFrame([],df_final_schema)
for cat_col in category_columns:
query="SELECT {d_name} as Country,SegmentID ,(count(*) over(partition by {d_name},SegmentID)/ count(*) over(partition by NoOfSegments))*100 as total_cnt from input_temp order by {d_name},SegmentID".format(d_name=cat_col)
new_df=hc.sql(query)
df_final = df_final.union(new_df)
结果:
是否有任何方法可以传递所有字符串列并一次计算数据帧的上述结果?您可以使用
groupBy
(或groupBy
)尝试以下操作:
您可以使用
groupBy
(或groupBy
)尝试以下操作:
看来你需要一个
groupBy
。你考虑过这样的事情吗?是的,好的。让我试试。看来你需要一个groupBy
。你考虑过这样的事情吗?是的,好的。让我试试那个。非常感谢。这很有帮助,非常感谢。这很有帮助
from pyspark.sql import functions as F
total = df.select(F.sum("NoOfSegments")).take(1)[0][0]
df \
.groupBy("SegmentID", "Country") \
.agg(F.sum('NoOfSegments').alias('sums'))\
.withColumn('total_cnt', 100 * F.col('sums')/ F.lit(total)) \
.select('country', 'SegmentID', 'total_cnt') \
.sort('country', 'SegmentID').show()
# +---------+---------+---------+
# | Country|SegmentID|total_cnt|
# +---------+---------+---------+
# |Bangalore| 1| 10.0|
# |Bangalore| 2| 10.0|
# |Bangalore| 3| 15.0|
# | Delhi| 1| 10.0|
# | Delhi| 2| 10.0|
# | Delhi| 3| 10.0|
# | Pune| 1| 10.0|
# | Pune| 2| 10.0|
# | Pune| 3| 15.0|
# +---------+---------+---------+