Question

I need help Structuring data into certain dictionaries in Python to help answer a few questions....

I need help Structuring data into certain dictionaries in Python to help answer a few questions. I have a CSV file and i need help looping thru the data to get an output similar to this. where the keys are the region (for data_by_region) or country (for data_by_country) and the values are dictionaries where the years are the keys. Based on this information i need a few dictionaries one being called data_by_region and another called data_by_country. Her is a link to the CSV file if anyone can help:

https://docs.google.com/document/d/1v68_QQX7Tn96l-b0LMO9YZ4ZAn_KWDMUJboa6LEyPr8/edit?usp=sharing


"Western Africa" : {

2005: {
    "Population annual rate of increase (percent)": 2.6,
"Total fertility rate (children per women)": 6,
"Infant mortality for both sexes (per 1,000 live births)": 95.7,
"Life expectancy at birth for both sexes (years)": 49.3,
"Life expectancy at birth for males (years)": 48.4,
"Life expectancy at birth for females (years)": 50.2
},
2010: { 
    
    },
2015: {
    
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

Code:

import csv

with open("data.csv") as csv_file:

data = {}

csv_reader = csv.reader(csv_file, delimiter=',')

l_c = 0

for row in csv_reader:

if(l_c==0):

l_c+=1

continue

row[2] = int(row[2])

if row[1] not in data:

data[row[1]] = {}

if row[2] not in data[row[1]]:

data[row[1]][row[2]] = {}

data[row[1]][row[2]][row[3]] = float(row[4].replace(",", ""))

print(data)

Add a comment
Know the answer?
Add Answer to:
I need help Structuring data into certain dictionaries in Python to help answer a few questions....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I need help with Python Dictionaries please. Question: Write a function named "get_stat" that takes...

    Hi, I need help with Python Dictionaries please. Question: Write a function named "get_stat" that takes a key-value store as a parameter with strings as keys and integers as values. The keys include "Strength", "Constitution", "Defense", "Dexterity", "Intelligence", "Charisma", "Willpower", and "Luck" and each value is an integer between 0 and 255. This function should return the value for the "Strength" stat. Thanks!

  • Hi, I need help with Python Dictionaries please. Question: Write a function named "add_key_value" that takes...

    Hi, I need help with Python Dictionaries please. Question: Write a function named "add_key_value" that takes a key-value store as a parameter with strings as keys and integers as values. The function will add a key-value pair to the input store with a key of "slam" and a value of 39. There is no need to return any value. Thanks!

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • i read this story today and need help with few questions

    i read this story today and need help with few questions. sorry it a bit long. i actually not real understand it, especially ending. THE WHITE KNIGHTby Eric NicolOnce upon a time there was a knight who lived in a little castle on the edge of the forest of Life. One day this knight looked in the mirror and saw that he was a White Knight."Lo!" he cried. "I am the White Knight and therefore represent good. I am the...

  • I need help in these questions in 30 minutes, please answer them fast Question 17 Between...

    I need help in these questions in 30 minutes, please answer them fast Question 17 Between 25% and 33% of all returnees exit their firms in what period of time? One year Two years Three years Five years Question 18 Duplicating American competencies in foreign countries is called: Ethnocentrism Home replication strategy Local responsiveness Integration framework Question 19 When an upward price change causes no loss of demand, the price is said to be: Elastic Inelastic Question 20 Wal-Mart occasionally...

  • Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell...

    Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell variables, and .profile script file, echo SHELL, HOME, PATH, MAIL and TERM are predefined shell variables. You can use the value of a shell variable in a shell command putting $ in front of it. For example, to display the value of the HOME directory of the user, specify $HOME in the echo command like echo $HOME. Do not give just the value of...

  • Hi, please I need help, I have case study about TESCO I need answer for only...

    Hi, please I need help, I have case study about TESCO I need answer for only ONE question without copy from another website and i need long answer as you can ,please I really need it soon and I trust of you. this the case study: Environmental Sustainability at Tesco PLC., UK Tesco PLC is the largest retailer in the United Kingdom and the third-largest grocery retailer in the world, with operations in 14 countries. The company manages its business...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Can you please help me with this questions I need different answer then then one posted...

    Can you please help me with this questions I need different answer then then one posted on here.Thank You! Application Question #7 – Mitosis is a term that describes the division of the nucleus and the cell’s genetic material. The division of a cell’s cytoplasm and plasma membrane is called cytokinesis. Explain the differences in cytokinesis between plant cells and animal cells. A. How is cytokinesis achieved in animal cells? Explain:__________________________________ B. How is cytokinesis accomplished in plant cells? Explain:________________________________...

  • 14. Select the number of participants in the Beck & Watson study Group of answer choices...

    14. Select the number of participants in the Beck & Watson study Group of answer choices 8 13 22 35 15. Beck & Watson determined their final sample size via Group of answer choices coding saturation triangulation ethnography 16.Through their study, Beck & Watson determined Group of answer choices after a traumatic birth, subsequent births have no troubling effects after a traumatic birth, subsequent births brought fear, terror, anxiety, and dread Subsequent Childbirth After a Previous Traumatic Birth Beck, Cheryl...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT