Marcus Renno Marcus Renno - 2 months ago 30
SQL Question

Pandas reset_index() creating level0 column

I'm reading one

CSV
file eliminating the duplicates and exporting to a database.

The problem here is that it is creating a column called
level0
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

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)
...
Comments