gpt4 book ai didi

pandas - 如何将 Google 表格的行导入 Pandas,但使用列名?

转载 作者:行者123 更新时间:2023-12-02 16:55:53 26 4
gpt4 key购买 nike

在很多地方都有很好的说明,可以使用 gspread 将 Google 表格导入 Pandas DataFrame,例如:

# Open our new sheet and read some data.
worksheet = gc.open_by_key('...').sheet1

# get_all_values gives a list of rows.
rows = worksheet.get_all_values()

# Convert to a DataFrame and render.
import pandas as pd
df = pd.DataFrame.from_records(rows)
df.head()

问题在于此导入将第一行视为值而不是标题。

如何导入 DataFrame 并将第一行视为列名而不是值?

最佳答案

你可以做到

row=[[1,2,3,4]]*3
pd.DataFrame.from_records(row[1:],columns=row[0])
1 2 3 4
0 1 2 3 4
1 1 2 3 4

关于pandas - 如何将 Google 表格的行导入 Pandas,但使用列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com