Home > OS >  Deleting a row in a Pandas Dataframe adds a column to the Dataframe
Deleting a row in a Pandas Dataframe adds a column to the Dataframe

Time:01-16

I want to delete a single row in a dataframe which gets generated based on a csv, and after the row gets deleted, the file should be saved again. But I have the problem, that everytime I delete a row, a column with the name "Unknown 0.x" gets added and I have no idea why. Here is my delete function:

def delete_row(path, file_type, row):
    if file_type == 'csv':
        df = pd.read_csv(path)
        df.drop(row, inplace=True)
        df.to_csv(path)

This is the datafram after I deleted 2 rows:

I tried different approaches for deleting a row, even with converting it into other file types first. And when I try this with .json or .txt the deletion works. Only with .csv I have this problem

CodePudding user response:

This happens because every time you use df.to_csv(), the index is saved as a new column. You need to set index=False to solve the problem. So your function would become:

def delete_row(path, file_type, row):
    if file_type == 'csv':
        df = pd.read_csv(path)
        df.drop(row, inplace=True)
        df.to_csv(path, index=False) # --> You need to set index to false here

On a related note, if you have an index column in your csv file that you want to set as index, assign that column as index while reading the csv file. For example:

pd.read_csv(path, index_col=0)
  • Related