Marcus Renno Marcus Renno - 1 year ago 243
SQL Question

Pandas reset_index() creating level0 column

I'm reading one

file eliminating the duplicates and exporting to a database.

The problem here is that it is creating a column called
instead of resetting the index.

Here is my code

df = pd.read_csv('SampleData.csv', sep=';', encoding='latin1', low_memory=False)
df_projects = df['External'].drop_duplicates()
df_projects = df_projects.to_frame()
df_projects.rename(columns={'External': 'name'}, inplace=True)
df_projects = df_projects.reset_index()

con = create_engine('sqlite:///db.sqlite3')
df_projects.to_sql("inventory_projects", con, index=True, if_exists='replace')

Answer Source

You need add parameter drop=True to reset_index:

df_projects = df_projects.rename('name').to_frame()
df_projects = df_projects.reset_index(drop=True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download