0

I have a csv file with data like

Name Cost SKU QTY 
Julia 1    13  10
John  5    23   1
Julia 3    40   5

I would like to return a dictionary as:

{'Julia':'10', 'John':'1', 'Julia':'5'....}

My code is returning no duplicates as of now.

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
  • If you can read your `.csv` file using pandas, then you can try either `df.to_json()` or `df.to_dict()`. The problem is a dictionary won't accept duplicate keys. You may refer to this answer for more information: https://stackoverflow.com/questions/10664856/make-a-dictionary-with-duplicate-keys-in-python – Celius Stingher Dec 02 '19 at 18:00

2 Answers2

0

Run this:

dict(zip(df.Name,df.QTY)
razimbres
  • 4,715
  • 5
  • 23
  • 50
0

check this answer on stackoverflow (Creating a dictionary from a csv file?) first you have to read as dictonary and change values according to that, remember that dictionary contains only distinct keys if key already exists it will be replaced by new values on the fly.

Shreyanshu
  • 85
  • 8