Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to avoid headers repeating everytime I append in Pandas

Im trying to append values from stored values taken from users. Is there a way to hard code the headers ("Player Name", "Age" and "Number") so evertime I run the code these headers dont appear again in the csv.

Edit: I want the headers to appear the "First time" it runs and not the ones after

data = {'Player Name': [playersNames[0]],
        'Age': [Age[0]],
        'Number': [number],

        df = pd.DataFrame(data)
        df.to_csv("Info.csv",mode='a',index=False, header=True)
        print(df)

At the moment it does this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Player Name,Age,Number
x,15,73
Player Name,Age,Number
y,25,70

>Solution :

Try:

import pathlib

csvfile = pathlib.Path('Info.csv')
df.to_csv(csvfile, mode='a', index=False, header=not csvfile.exists())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading