Pandas to_sql() 插入索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43942357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:35:36  来源:igfitidea点击:

Pandas to_sql() inserting index

pythonpandassqlalchemy

提问by AlexanderMP

I am using Pandas 0.18.1, and while fiddling with this code,

我正在使用 Pandas 0.18.1,在摆弄这段代码的同时,

import pd

def getIndividualDf(item):
    var1 = []
    # ... populate this list of numbers
    var2 = []
    # ... populate this other list of numbers

    newDf = pd.DataFrame({'var1': var1, 'var2': var2})
    newDf['extra_column'] = someIntScalar
    yield newDf

dfs = []
for item in someList:
    dfs.append(getIndividualDf(item))

resultDf = pd.concat(dfs)
resultDf['segment'] = segmentId # this is an integer scalar

from sqlalchemy import create_engine
engine = create_engine('postgresql://'+user+':'+password+'@'+host+'/'+dbname)
resultDf.reset_index().to_sql('table_name', engine, schema="schema_name", if_exists="append", index=False)

I was getting this exception:

我收到此异常:

(psycopg2.ProgrammingError) column "index" of relation "table_name" does not exist

(psycopg2.ProgrammingError) 关系“table_name”的“index”列不存在

Indeed, there is no such column in the table, only because there is no such explicit column in the data frame. Which is why it's weird.

确实,表中没有这样的列,只是因为数据框中没有这样的显式列。这就是为什么它很奇怪。

Running

跑步

print(list(resultDf))

just before the to_sql()call, yields

就在to_sql()调用之前,产生

['var1', 'var2', 'extra_column', 'segment']

['var1', 'var2', 'extra_column', 'segment']

Removing index=Falsefrom the to_sql()call changes the error to this:

index=Falseto_sql()调用中删除将错误更改为:

(psycopg2.ProgrammingError) column "level_0" of relation "table_name" does not exist

(psycopg2.ProgrammingError) 关系“table_name”的“level_0”列不存在

I am puzzled. How do I get rid of indexcolumn?

我很困惑。我如何摆脱index列?

Update
print(resultDf.head())yielded this information:

更新
print(resultDf.head())产生了以下信息:

     var1       var2  extra_column  segment
0       8   0.101653    2077869737   201606
1       9   0.303694    2077869737   201606
2      10   0.493210    2077869737   201606
3      11   0.661064    2077869737   201606
4      12   0.820924    2077869737   201606

回答by Steven G

You need not to reset the index before writing to sql such has:

您无需在写入 sql 之前重置索引,例如:

resultDf.to_sql('table_name', engine, schema="schema_name", if_exists="append", index=False)