通过pygsheets写入Gsheet时自动删除逗号

2024-09-27 00:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过pygsheets库将Pandas DF写入GSheet

brand_list = [
["Progressive",["Progressive"],["Progressive","PROGRESSIVE"]],

["USAA",["USAA"],["USAA","Usaa"]],

["State Farm",["State Farm"],["State Farm","STATE FARM"]],

["Geico",["Geico","GEICO","Geico - Renters Insurance","Geico - App"],["Geico","GEICO"]],

["Allstate",["Allstate"],["Allstate","ALLSTATE"]],

["Dell",["Dell"],["DELL","Dell"]],

["Lenovo",["Lenovo"],["LENOVO"]],

["HP",["HP"],["HP"]],

["Apple",["Apple","Apple iPhone","Apple - iPhone 12 Pro","Apple - iPhone","Apple TV+","Apple TV+ - Palmer","Apple - MacBook","Apple TV+ - The Snoopy Show","Apple TV+ - Various","Apple Music","Apple TV+ - Losing Alice","Apple - Apple Watch Series 6","Apple TV+ - Greyhound"],["APPLE"]]]

customSchema = StructType([
  StructField("ParentCompany", StringType(), True),
  StructField("BrandName1", ArrayType(StringType()), True), 
  StructField("BrandName2", ArrayType(StringType()), True)
])

df1 = spark.createDataFrame(brand_list,schema=customSchema)
df1_pd = df1.toPandas()

这是我的示例数据集。 现在我试图通过pygsheets将此df推送到gsheet

import pygsheets
gc = pygsheets.authorize(service_file = 'xxxxxxxx.json')
google_workbook='Demo Data'
google_sheet='Sheet1'

sheet = gc.open(google_workbook)
wks = sheet.worksheet_by_title(google_sheet)

wks.set_dataframe(df1_pd,(1,1),copy_head=True)

但是当我写数据时,数据会被写入到工作表中,但是数组中项目之间的逗号会丢失。附上问题的屏幕截图,有人能帮助我如何在不丢失逗号的情况下写入值吗

enter image description here


Tags: trueapplegoogletvdellsheethpprogressive

热门问题