1

I would like to add a column to a given index with a different value at each time (that value is computed depending on the values of the row). This is a sample of my csv:

org,repo_name,stars_count,fork_count,commit_count
freeCodeCamp,freeCodeCamp,303178,22005,23183,1703
vuejs,vue,140222,20150,3016,82
twbs,bootstrap,133730,65555,18714,46
...

So far I tried the answer provided here: python pandas insert column

def func(f):
    files = f
    df = pd.read_csv(files)
    df = df.convert_objects(convert_numeric=True)
    df.insert(2, 'new', 1000)
    df.to_csv(files) 

I get the result of an added row to index 2 with values 1000.

,org,repo_name,new,stars_count,fork_count,commit_count
freeCodeCamp,freeCodeCamp,303178,1000,22005,23183,1703
vuejs,vue,140222,1000,20150,3016,82
twbs,bootstrap,133730,1000,65555,18714,46
...

How to modify this to be able to add a specific value to each row instead of adding 1000 everywhere? And how to add a header so I get the following output? Please note that score1... scoreN are int variables, not string and that you can assume that they already been computed.

org,repo_name,score,new,stars_count,fork_count,commit_count
freeCodeCamp,freeCodeCamp,303178,score1,22005,23183,1703
vuejs,vue,140222,score2,20150,3016,82
twbs,bootstrap,133730,score3,65555,18714,46
...

Thanks.

7
  • How you want your csv to look like? Let us know more about it. Commented Jun 4, 2019 at 6:33
  • @AmazingThingsAroundYou Hi, I gave the output that i want at the end (my last code snippet). Is that what you are referring to? Commented Jun 4, 2019 at 6:39
  • df.insert(2,'new',['score{}'.format(i+1) for i in range(len(df))]) ? Commented Jun 4, 2019 at 7:12
  • @anky_91, hi, I've edited my post since it seems like it wasn't clear enough. score is not a string, it's a int that is different for each rows. Commented Jun 4, 2019 at 7:18
  • @SoyänChardon how are you getting score? Commented Jun 4, 2019 at 7:20

2 Answers 2

1

You can try something like this:

len_df = len(df.index)+1
df["new"] = ["score"+str(i) for i in range(1,len_df)]

I hope this will help you. ok so this might will be helpful:

df["new"].values[2] = score_value

Note that score_value is int

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

2 Comments

Hi, it maybe wasn't clear but score is not a string, it's a int variable that is different for each row, so unfortunately your answer doesn't help. i've edited my post to avoid misunderstandings.
@SoyänChardon I hope now my edited answer will help you.
0

Pandas is close to overkill to only insert a new column into a csv:

with open('input.csv') as fdin, open('output.csv', 'w', newline='') as fdout:
    rd = csv.DictReader(fdin)
    fields = list(rd.fieldnames)
    fields.insert(2, 'new')
    wr = csv.DictWriter(fdout, fieldnames=fields)
    wr.writeheader()
    for row in rd:
        row['new'] = compute_val(row)    # or compute_val(*row)
        wr.writerow(row)

3 Comments

Hi, thanks for your answer. Unfortunately it doesn't work as instead of inserting a column at index 2 and therefore shifting to the right the others columns, it overwrite the content of the column at index 2. The output is freeCodeCamp,freeCodeCamp,0.0628325397113628,22015,23213,1670, instead of freeCodeCamp,freeCodeCamp,0.0628325397113628,303198,22015,23213,1670
@SoyänChardon: It worked in my tests. Maybe you should use fields = list(rd.fieldnames) to ensure to get a true list. I have edited my post.
@SergeBellesta Thanks you very much, it now works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.