【问题标题】:Get data from csv and calculate average从 csv 获取数据并计算平均值
【发布时间】:2019-09-18 11:16:28
【问题描述】:

首先我需要使用 python spark 从 csv 文件中计算一列的平均值,

我有一个代码:

 scSpark = SparkSession \
.builder \
.appName("Python Spark SQL basic example: Reading CSV file without mentioning schema") \
.config("spark.some.config.option", "some-value") \
.getOrCreate()

sdfData = scSpark.read.csv("document.csv", header=True, sep=",")
sdfData.show()

然后我在屏幕上获取下一个数据:

   +---------+------+---------+------------------+
   |     Name| total| test val|             ratio|
   +---------+------+---------+------------------+
   |parimatch|     3|   test7 |0.6164045285312666|
   |parimatch|     4|   test6 |0.5829715240832467|
   |     leon|     3|   test5 |0.6164045285312666|
   |     leon|     4|   test4 |0.5829715240832467|
   |parimatch|     3|   test3 |0.6164045285312666|
   |parimatch|     4|    test |0.5829715240832467|
   +---------+------+---------+------------------+

如何通过 spark 计算比率之间的平均值?

【问题讨论】:

    标签: apache-spark pyspark pyspark-sql


    【解决方案1】:

    Apache Spark 具有执行此操作的平均功能:

    import pyspark.sql.functions as f
    
    average = sdfData.agg(avg(col("ratio")))
    

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2019-12-03
      相关资源
      最近更新 更多