在pyspark中使用groupby时无法获取所有列

在pyspark中使用groupby时无法获取所有列,pyspark,pyspark-sql,Pyspark,Pyspark Sql,像这样使用时,我遇到以下错误: columnList = [item[0] for item in df1.dtypes if item[1].startswith('string')] df2 = df1.groupBy("TCID",columnList).agg(mean("Runtime").alias("Runtime")) 从pyspark.sql.DataFrame.groupBy获取“要分组的列列表” 代码失败,因为第二个参数(columnList)不是有效的列标识符。因此错

像这样使用时,我遇到以下错误:

columnList = [item[0] for item in df1.dtypes if item[1].startswith('string')]

df2 = df1.groupBy("TCID",columnList).agg(mean("Runtime").alias("Runtime"))
pyspark.sql.DataFrame.groupBy
获取“要分组的列列表”

代码失败,因为第二个参数(
columnList
)不是有效的列标识符。因此错误:
col([class java.util.ArrayList])不存在

相反,您可以执行以下操作:

df2=df1.groupBy([“TCID”]+columnList).agg(平均值(“运行时”).别名(“运行时”))
或同等,且更易于阅读:

columnList=[item[0]用于df1中的项。如果项[1],则类型为。startswith('string')]
groupByColumns=[“TCID”]+columnList
df2=df1.groupBy(groupByColumns).agg(平均值(“运行时”).alias(“运行时”))

Try
df2=df1.groupBy([“TCID”]+columnList)….
谢谢@pault,工作正常。如何从columnList中删除TCID使用
df。选择(columnList)
以选择所需的列。如果这不是你的意思,你能举一个你想要的输出的例子吗?更多信息:我在列列表中有TCID。我将TCID作为groupBy提供,因此它在结果中显示了两次。可以删除吗?您希望按哪列分组?只有TCID,还是所有列?请尝试提供一个小例子。
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)