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.
df.insert(2,'new',['score{}'.format(i+1) for i in range(len(df))])?