Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Add column to CSV using Pandas
#1
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)
Larz60+ wrote Apr-07-2020, 01:37 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. Refer to BBCode help topic on how. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Quote
#2
@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
Quote
#3
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 156 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  add formatted column to pandas data frame alkaline3 0 182 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  itertuples, new column, datetime, pandas karlito 6 597 Nov-29-2019, 11:07 AM
Last Post: karlito
  sort values of a column pandas karlito 2 534 Oct-22-2019, 06:11 AM
Last Post: karlito
  Dropping a column from pandas dataframe marco_ita 6 4,209 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 777 Aug-26-2019, 06:36 PM
Last Post: snippsat
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 494 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 1,335 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison
  pandas change row value an existing column with conditionals Gigux 1 753 Jun-22-2019, 08:04 PM
Last Post: Gigux
  How to delete column if entire column values are "nan" Sri 4 1,016 Apr-13-2019, 12:16 PM
Last Post: Sri

Forum Jump:


Users browsing this thread: 1 Guest(s)