0

I have the following dataframe:

Country    Qty     

Belgium    54                       
Belgium    8                      
Belgium    67                      
France     12                       
France     3                      
France     34
Italy      25
Italy      45
Italy       9

Is it possible to groupBy this dataframe by column "Country", aggregate average of the "Qty" output average Qty by Belgium? I am using Spark Python.

2 Answers 2

1

This has been solved!

df.filter(df['country'] == 'Belgium').agg(avg(col("Qty")
Sign up to request clarification or add additional context in comments.

1 Comment

The answer need more description.
0
from pyspark.sql import functions as F

(
    df
    .groupBy("Country")
    .agg(F.mean("Qty").alias("avg"))
    .filter(F.col("Country") == "Belgium")
    .show()
)

# output
+-------+----+
|Country| avg|
+-------+----+
|Belgium|43.0|
+-------+----+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.