如何将数据帧结果保存到数据砖中的表中?



我正在尝试将已转换为数据帧的单词列表保存到数据砖中的表中,以便以后在群集重新启动时可以查看或引用它。

我尝试了以下代码,但它一直给我一个错误或确实运行,但我看不到数据库中的表

myWords_External=[['this', 'is', 'my', 'world'],['this', 'is', 'the', 'problem']]
df1 = pd.DataFrame(myWords_External)
df1.write.mode("overwrite").saveAsTable("temp.eehara_trial_table_9_5_19")

最后一行给我以下错误

AttributeError: 'DataFrame' object has no attribute 'write'

我认为您在代码中混淆了两种不同的技术。第一部分是熊猫:

myWords_External=[['this', 'is', 'my', 'world'],['this', 'is', 'the', 'problem']]
df1 = pd.DataFrame(myWords_External)

第二部分是 pyspark:

df1.write.mode("overwrite").saveAsTable("temp.eehara_trial_table_9_5_19")

我不知道您的用例是什么,但假设您想使用 pandas 并且您不知道如何连接到底层数据库,这是将 pandas 数据帧转换为 pyspark 数据帧并将其另存为表的最简单方法:

spark_df = spark.createDataFrame(df1)
spark_df.write.mode("overwrite").saveAsTable("temp.eehara_trial_table_9_5_19")
#you can create a new pandas dataframe witht the following command:
pd_df = spark.sql('select * from temp.eehara_trial_table_9_5_19').toPandas()

这是低效的,最好使用 pandas.to_sql 函数,但您需要知道提供的数据库和连接凭据。

最新更新