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

Pandas dataframe throwing error when appending to CSV

`

import pandas as pd

df = pd.read_csv("stack.csv")

sector_select = "Col2"

df[sector_select] = ["100"]

df.to_csv("stack.csv", index=False, mode='a', header=False)

`

stack.csv has no data other than a header: Col1,Col2,Col3,Col4,Col5

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

ValueError: Length of values (1) does not match length of index (2)

Im just trying to make a program where I can select a header and append data to the column under that header

You can only run it twice until it gives an error!

>Solution :

That code runs for me.

But I assume that you would like to run something like this:

import pandas as pd

df = pd.read_csv("stack.csv")

sector_select = "Col2"

df.at[len(df), sector_select] = "100"

df.to_csv("stack.csv", index=False)
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