Home > database >  How to add an extra column to all rows in a dictionary from a given list?
How to add an extra column to all rows in a dictionary from a given list?

Time:03-24

Here is my program to make a dictionary from a given list:

import csv

list1=[]
header=[]

with open('D:\C  \Programs\Advanced Programming\grades.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    for line in csv_reader:
        list1.append(line)

header = list1[0]

res = [dict(zip(header, values)) for values in list1[1:]]

for i in res:
    print(i)

the output is:

{'Last name': 'Alfalfa', ' First name': '   Aloysius', ' Final': '49', ' Grade': '   D-'}
{'Last name': 'Alfred', ' First name': '    University', ' Final': '48', ' Grade': '   D '}
{'Last name': 'Gerty', ' First name': '     Gramma', ' Final': '44', ' Grade': '   C'}
{'Last name': 'Android', ' First name': '   Electric', ' Final': '47', ' Grade': '   B-'}
{'Last name': 'Bumpkin', ' First name': '   Fred', ' Final': '45', ' Grade': '   A-'}
{'Last name': 'Rubble', ' First name': '    Betty', ' Final': '46', ' Grade': '   C-'}

Now to this dictionary I have to add another column total marks which should contain total marks of the student which is there in the list i.e list[2]

How can i simultaneously add all the marks to the dictionary so that it will look like:

{'Last name': 'Alfalfa', ' First name': '   Aloysius', ' Final': '49', ' Grade': '   D-', 'Total marks': '49'}
{'Last name': 'Alfred', ' First name': '    University', ' Final': '48', ' Grade': '   D ','Total marks': '48'}

I don't understand how to make it. Please tell me how to solve this.

CodePudding user response:

The solution is to treat your data as pandas dataFrame instead of a list.

As an example:

  import pandas as pd
  data = [{'Last name': 'Alfalfa', ' First name': '   Aloysius', ' Final': '49', 
  ' Grade': '   D-'},{'Last name': 'Alfred', ' First name': '    University', ' 
   Final': '48', ' Grade': '   D '}]
   df=pd.DataFrame.from_dict(data,orient='columns')
   list2=['49','48']
   df['total marks']=list2
   df

result

CodePudding user response:

As i understand from this, you want to add a new column with a value equals to ' Final', you could do something like this:

for raw in res:
    raw['Total marks'] = raw[' Final']
print(res)

CodePudding user response:

I don't know how you store the marks so I can't provide you with a total working answer, but in short:

res is a list of dictionaries. you can add an extra key and value to a dictionary by just setting it: my_dict['my_key'] = 'my_value'. You should use this in your for loop like so:

for i in res:
    i['Total marks'] = total_marks # where total_marks is the total number.

Does this help you?

If list2 contains all total marks in the same order as the students in list1 are, you can:

for i in range(len(res):
    res[i]['Total marks'] = list2[i]

CodePudding user response:

for dict in res:
    dict['Total marks'] = dict[' Final']

also, you might want to make the numerical values integers/floats, not strings.

  • Related