python - 如何修复 'DataFrame' 对象没有属性 'coalesce'?

标签 python apache-spark dataframe pyspark apache-spark-sql

在 PySpark 应用程序中,我尝试通过将数据帧转换为 pandas 来转置它,然后我想将结果写入 csv 文件。我是这样做的:

df = df.toPandas().set_index("s").transpose()
df.coalesce(1).write.option("header", True).option("delimiter", ",").csv('dataframe')

执行此脚本时出现以下错误:

'DataFrame' object has no attribute 'coalesce'

问题是什么?我该如何解决?

最佳答案

问题是您将 spark 数据帧转换为 pandas 数据帧。 Pandas 数据框没有 coalesce 方法。您可以查看 pandas 的文档 here .

当您使用 toPandas() 时,数据帧已经收集并在内存中, 尝试改用 pandas 数据框方法 df.to_csv(path)

关于python - 如何修复 'DataFrame' 对象没有属性 'coalesce'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46464483/

相关文章:

python - python 3.5 上的 PyHook

apache-spark - 创建包含 Spark 数据帧字段中数组中每个结构的第一个元素的数组

python - 在python中将多个数据框合并为一个数据框

Pandas 将切割中的列添加到 DataFrame

python - 使用 Python 日志记录模块时出现重复的日志输出

python - 获取类变量和值的字典

scala - 如何使用 withColumn Spark Dataframe scala with while

java - 从嵌套的用户定义对象中创建 DataFrame

python - TypeError : Cannot convert value <tensorflow. python.keras.losses.CategoricalCrossentropy 对象 ...> 到 TensorFlow DType

scala - 由 : java. lang.IllegalArgumentException 引起:无法为 null 获取 JDBC 类型