Home > database >  Extra column appears when appending selected row from one csv to another in Python
Extra column appears when appending selected row from one csv to another in Python

Time:02-21

I have this code which appends a column of a csv file as a row to another csv file:

def append_pandas(s,d):
    import pandas as pd
    df = pd.read_csv(s, sep=';', header=None)
    df_t = df.T
    df_t.iloc[0:1, 0:1] = 'Time Point'
    df_t.at[1, 0] = 1
    df_t.columns = df_t.iloc[0]
    df_new = df_t.drop(0)
    pdb = pd.read_csv(d, sep=';')
    newpd = pdb.append(df_new)
    from pandas import DataFrame
    newpd.to_csv(d, sep=';')

The result is supposed to look like this:

enter image description here

Instead, every time the row is appended, there is an extra "Unnamed" column appearing on the left:

enter image description here

Do you know how to fix that?.. Please, help :(

My csv documents from which I select a column look like this:

enter image description here

CodePudding user response:

You have to add index=False to your to_csv() method

  • Related