Reputation: 381
columnList = [item[0] for item in df1.dtypes if item[1].startswith('string')]
df2 = df1.groupBy("TCID",columnList).agg(mean("Runtime").alias("Runtime"))
While using like this I am getting the following error :
py4j.protocol.Py4JError: An error occurred while calling z:org.apache.spark.sql.functions.col. Trace:
py4j.Py4JException: Method col([class java.util.ArrayList]) does not exist
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:339)
at py4j.Gateway.invoke(Gateway.java:274)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Thread.java:748)
Upvotes: 2
Views: 2308
Reputation: 43534
From the docs pyspark.sql.DataFrame.groupBy
takes in a "list of columns to group by."
Your code fails because the second argument (columnList
) isn't a valid column identifier. Hence the error: col([class java.util.ArrayList]) does not exist
.
Instead you can do the following:
df2 = df1.groupBy(["TCID"] + columnList).agg(mean("Runtime").alias("Runtime"))
Or equivalently, and easier to read IMO:
columnList = [item[0] for item in df1.dtypes if item[1].startswith('string')]
groupByColumns = ["TCID"] + columnList
df2 = df1.groupBy(groupByColumns).agg(mean("Runtime").alias("Runtime"))
Upvotes: 2