0

I am trying to add new columns to an existing csv that already has rows and columns that looks like this: enter image description here

I would like it to append all the new column names to the columns after column 4.

The code I currently have is adding all the new columns to the bottom of the csv:

def extract_data_from_report3():
    with open('OMtest.csv', 'a', newline='') as f_out:
        writer = csv.writer(f_out)
        writer.writerow(
            ['OMGroup:OMRegister', 'OMGroup', 'OMRegister', 'RegisterType', 'Measures', 'Description', 'GeneratedOn'])
      

Is there any way to do this effectively?

2
  • Does this answer your question? How to add a new column to a CSV file? Commented Oct 20, 2020 at 18:41
  • 1
    Also see my answer to a related question — it's relatively easy with a csv.DictReader and csv.DictWriter. Commented Oct 20, 2020 at 18:43

1 Answer 1

2

You can use the pandas lib, without iterating through the values. Here an example

new_header = ['OMGroup:OMRegister', 'OMGroup', 'OMRegister', 'RegisterType', 'Measures', 'Description', 'GeneratedOn']
# Import pandas package  
import pandas as pd 

my_df = pd.read_csv(path_to_csv)
for column_name in new_header:
    new_column = [ ... your values ...] #should be a list of your dataframe size
    my_df[column_name] = new_column

keep in mind that the new column should have the same size of the number of rows of your table to work

If you need only to add the new columns without values, you can do as such:

for column_name in new_header:
    new_column = ["" for i in range(len(mydf.index))] #should be a list of dataframe size
    my_df[column_name] = new_column

Then you can write back the csv in this way:

my_df.to_csv(path_to_csv)

Here details on the read_csv method

Here details on the to_csv method

Sign up to request clarification or add additional context in comments.

11 Comments

@marcorivera8 yes, you are adding a new column, not a new row
@marcorivera8 keep in mind that here my_df['OMGroup:OMRegister'] I am using the first value of your list as column name. I am assuming that's the column name
As I can see from the list ['OMGroup:OMRegister', 'OMGroup', 'OMRegister', 'RegisterType', 'Measures', 'Description', 'GeneratedOn'] seems more a new header than actually values in your rows
I am incorporating this code with BeautifulSoup and XML data which is where the row content is coming from. I'll let u know in a bit if I get it to work properly. Thanks for the help!
@marcorivera8 I unfortunately don't know beautiful soup. I suggest you to open a new question (it is better to not expand your problem further on this question, to keep the question specific and not too broad). Please don't forget to accept the best answer for your first question
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.