Python Forum
Add column to CSV using Pandas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Add column to CSV using Pandas (/thread-25654.html)



Add column to CSV using Pandas - nsadams87xx - Apr-06-2020

Hey everyone,

I have a list that I made as a dataframe and I want to take this dataframe and add it to an existing CSV file with data already in it.

The csv has three columns already,

Date,Confirmed,Deaths
2/29/2020,2,0
3/1/2020,3,0
3/2/2020,4,0
3/3/2020,4,0
3/4/2020,4,0
3/5/2020,5,0
3/6/2020,5,0
3/7/2020,6,0
3/8/2020,7,0
3/9/2020,7,0
...

My column I want to add is "New Cases". Yes, you can guess what this is about..... Wall

I used this code and figured out quickly that this overwrites the whole CSV:

df = pd.DataFrame(data={"New_Cases": case_list})
df.to_csv("./Cook County.csv", sep=',',index=False)


RE: Add column to CSV using Pandas - UGuntupalli - Apr-15-2020

@nsadams87xx,
First suggestion is to follow Larz60+ advice and reformat your question. As for your original problem, a very easy way to fix that would be:
  1. Use Pandas to read the original csv into a dataframe
  2. Concatenate your original dataframe to the dataframe you created from the csv
  3. Write the new dataframe to a csv



RE: Add column to CSV using Pandas - snippsat - Apr-15-2020

As mention bye @UGuntupall so is the first step to get the .csv read into a DataFrame.
Then should a search help as adding a new column to a DataFram is very basic task in Pandas.
Here Notebook you can look at.