say I have a dataframe like this
name age city
abc 20 A
def 30 B
i want to add a summary row at the end of the dataframe, so result will be like
name age city
abc 20 A
def 30 B
All 50 All
So String 'All', I can easily put, but how to get sum(df['age']) ###column object is not iterable
data = spark.createDataFrame([("abc", 20, "A"), ("def", 30, "B")],["name", "age", "city"])
data.printSchema()
#root
#|-- name: string (nullable = true)
#|-- age: long (nullable = true)
#|-- city: string (nullable = true)
res = data.union(spark.createDataFrame([('All',sum(data['age']),'All')], data.columns)) ## TypeError: Column is not iterable
#Even tried with data['age'].sum() and got error. If i am using [('All',50,'All')], it is doing fine.
I usually work on Pandas dataframe and new to Spark. Might be my undestanding about spark dataframe is not that matured.
Please suggest, how to get the sum over a dataframe-column in pyspark. And if there is any better way to add/append a row to end of a dataframe. Thanks.