gpt4 book ai didi

python - pangres python : IndexError: All index levels must be named

转载 作者:行者123 更新时间:2023-12-04 17:18:55 40 4
gpt4 key购买 nike

我想使用“pangres”在我的 sql 数据库中做一个更新插入,然而它返回这个错误

raise IndexError("All index levels must be named!")
IndexError: All index levels must be named!

我该怎么办?

df = pd.read_excel('personne.xlsx')
upsert(engine=engine,
df=df,
table_name='personne',
if_row_exists='update')

最佳答案

命名 df 索引或将列设置为索引。参见 the Pangres wiki .

对于将 df['unique_id'] 用作主键的 dfs:

# create SQL table with first df, set if_exists as needed
df.to_sql('table_name', engine, if_exists = 'fail', index = False)

with engine.connect() as con: # set df['unique_id'] as primary key in SQL
con.execute("ALTER TABLE table_name ADD PRIMARY KEY (unique_id);")

# set df2['unique_id'] as index
df2.set_index(['unique_id'], inplace = True, drop = False)
pangres.upsert(engine, df2, 'table_name', if_row_exists ='update')

关于python - pangres python : IndexError: All index levels must be named,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67367272/

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