我正在尝试在以下脚本中使用我在googlespreadsheet中追加新行

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread_dataframe import get_as_dataframe, set_with_dataframe
import pandas as pd

ws = gc.open("test_submit").worksheet("Oct-2019")

d = {'Name': ['T', 'Z'], 'ID': [3, 4]}
df = pd.DataFrame(data=d)
existing = get_as_dataframe(ws)
existing = existing.dropna(how='all')
updated = existing.append(df)
set_with_dataframe(ws, updated)


但会在空白列标题中创建名称为unnamed0,unnamed1 .... unnamed的不必要的列。而且,它不会以现有列名的正确格式追加行。任何帮助表示赞赏

最佳答案

我已经使用以下脚本解决了我的问题

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread_dataframe import get_as_dataframe, set_with_dataframe
import pandas as pd

ws = gc.open("test_submit").worksheet("Oct-2019")

d = {'Name': ['T', 'Z'], 'ID': [3, 4]}
df = pd.DataFrame(data=d)
existing = get_as_dataframe(ws)
existing = existing.dropna(how='all')
new_df= pd.DataFrame()
new_df = new_df.append(existing)
new_df = new_df.append(df)
set_with_dataframe(ws, new_df,row=1, col=1, include_index=False, include_column_header=True,
                       resize=False, allow_formulas=True)

07-24 13:14