首页 > 解决方案 > Pandas Concat - gspread 工作表

问题描述

我正在尝试连接工作表列表。我已经使用client.open_by_url().

我努力了:

worksheet_list = spreadsheet.worksheets()
df = pd.concat(worksheet_list, ignore_index=True)

但是得到这个错误:"TypeError: cannot concatenate object of type '<class 'gspread.models.Worksheet'>'; only Series and DataFrame objs are valid"

问题是有人知道如何连接该gspread.models类型的工作表。

任何帮助都非常感谢提前。谢谢。

标签: pythonpandasdataframeconcatenationgspread

解决方案


工作表对象的类型错误concat。您需要在合并之前将其更改为 pandas DataFrame。

 #Iterate to get dataframes.
 list_of_dfs = [pd.DataFrame(ws.get_all_records()) for ws in worksheet_list] 

 # Concat the list of dataframes
 df = df.concat(list_of_dfs, ignore_index=True)

推荐阅读