使用Pandas在Google电子表格中使用现有行附加新行

时间:2019-10-31 08:37:02

标签: python pandas google-sheets gspread

我正在尝试在脚本下面使用我在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的不必要的列。而且,它不会以现有列名的正确格式追加行。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

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

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)