0

I'm trying to remove an empty column from a .csv file that I generate. As can be seen, the first column is empty, I want to remove this but have failed to do so using the pd.drop() feature. Using the pd.info() tells me nothing about the first column. CSV File

CSV data

Info simply just shows that "0_x" is the first column, but it for sure isn't. This is the simple code I've used so far for this:

def transform_to_test():
    df1 = pd.read_csv('csv-data/testfile.csv')
    df2 = pd.read_csv('csv-data/testlabelfile.csv')

    new_df = pd.merge(df1, df2[['Unnamed: 0', '0']], on='Unnamed: 0', how='left')
    new_df = new_df.drop(['Unnamed: 0'], axis=1)
    print(new_df.info())
    new_df.to_csv(r'testing.csv')

I've tried adding the empty string '' to the drop function, but it produces an error:

KeyError: "[''] not in index"

Hope you can help

  • First column is index, for avoid write to file use `new_df.to_csv(r'testing.csv', index=False)` – jezrael Apr 17 '23 at 13:04

0 Answers0