2
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)
Praveen Mandadi
  • 371
  • 3
  • 24
  • 2
    Try `df2 = df1.groupBy(["TCID"] + columnList) ...` – pault Jan 29 '18 at 15:50
  • thanks @pault, Its working fine. How can I drop the TCID from columnList – Praveen Mandadi Jan 29 '18 at 15:56
  • Use `df.select(columnList)` to pick the columns you want. If that's not what you meant, can you show an example of your desired output? More info: [How to make good reproducible apache spark examples](https://stackoverflow.com/questions/48427185/how-to-make-good-reproducible-apache-spark-dataframe-examples) – pault Jan 29 '18 at 16:02
  • I'm having TCID in columnList. I'm giving TCID as groupBy so it is showing twice in the result. Can that be dropped? – Praveen Mandadi Jan 29 '18 at 16:10
  • Which column do you want to group by? Only TCID, or all the columns? Please try to provide a small example. – pault Jan 29 '18 at 16:12
  • I want do the groupby function only for TCID but in result I need all the columns – Praveen Mandadi Jan 29 '18 at 16:15

1 Answers1

2

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"))
Alper t. Turker
  • 34,230
  • 9
  • 83
  • 115
pault
  • 41,343
  • 15
  • 107
  • 149