【问题标题】:Append new row in google spreadsheet with existing rows using pandas使用 pandas 在 google 电子表格中添加现有行的新行
【发布时间】:2019-10-31 08:37:02
【问题描述】:

我正在尝试在 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 的空白列标题中创建不必要的列。此外,它不会以现有列名的正确格式附加行。任何帮助表示赞赏

【问题讨论】:

  • 我知道我的回答不适合你的问题。所以我必须删除我的答案,因为我不想混淆其他用户。我为我糟糕的技术深表歉意。我想学习更多。

标签: python pandas google-sheets gspread


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多