Mapping Keys from a List of Nested Dictionaries to Columns in a DataFrame from a JSON fileHow to remove a key from a Python dictionary?Delete column from pandas DataFrameHow to return dictionary keys as a list in Python?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersConvert list of dictionaries to a pandas DataFrameSplitting dictionary/list inside a Pandas Column into Separate ColumnsFilter dataframe with dictionary values while assigning dictionary keys to matching rows?Unfold a nested dictionary with lists into a pandas DataFrame
Is a request to book a business flight ticket for a graduate student an unreasonable one?
definition of "percentile"
Finding the nth term of sequence of 3, 10, 31, 94, 283...
Is "I do not want you to go nowhere" a case of "DOUBLE-NEGATIVES" as claimed by Grammarly?
C program to parse source code of another language
Was I subtly told to resign?
How can a dictatorship government be beneficial to a dictator in a post-scarcity society?
Using Newton's shell theorem to accelerate a spaceship
Print the last, middle and first character of your code
Why were Er and Onan punished if they were under 20?
Does throwing a penny at a train stop the train?
QGIS Zanzibar how to crop?
How did the hit man miss?
Credit score and financing new car
How to tell someone I'd like to become friends without letting them think I'm romantically interested in them?
Are neural networks prone to catastrophic forgetting?
Referring to different instances of the same character in time travel
How were Martello towers supposed to work?
Received a dinner invitation through my employer's email, is it ok to attend?
Single word for "refusing to move to next activity unless present one is completed."
What is the job of the acoustic cavities inside the main combustion chamber?
During copyediting, journal disagrees about spelling of paper's main topic
Why isn't there research to build a standard lunar, or Martian mobility platform?
Is anyone advocating the promotion of homosexuality in UK schools?
Mapping Keys from a List of Nested Dictionaries to Columns in a DataFrame from a JSON file
How to remove a key from a Python dictionary?Delete column from pandas DataFrameHow to return dictionary keys as a list in Python?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersConvert list of dictionaries to a pandas DataFrameSplitting dictionary/list inside a Pandas Column into Separate ColumnsFilter dataframe with dictionary values while assigning dictionary keys to matching rows?Unfold a nested dictionary with lists into a pandas DataFrame
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to work with two columns in a dataframe created from a JSON file. One column contains strings of country names, the other column contains a list of dictionaries. Each dictionary represents a given economic or socioeconomic project. Each list varies in size. Each dictionary contains two keys, 'code' and 'name', and there exists only one value for each key. The value for code is a string number i.e. '8' and the value for name is a string title i.e. 'human development'.
So, each country in the country column can appear more than once and with a list of dictionaries in the projects column. Most likely, there are redundant dictionaries. I would like to parse the information into two dataframes.
First, I would like to bag all the code values in a list so that when a country name appears in the country column, there is a list of code values right beside it in the projects column. When I am done gathering all the code values, I will have to count the number of distinct code values that correspond to a given country.
One dataframe should not have its dimensions altered. It should have the name of a country in the country column and the projects column should have a list of string numbers from the code key (already contained in the list of dictionaries for that element), instead of a list of dictionaries. So, one list of code values per country, even if the country appears more than once.[HELP]
The other dataframe will have its dimensionality altered. It will have all the DISTINCT code values collected in a list inside the projects column for a given country in the country column, instead of several appearances of a given country with potentially redundant elements from its list of string numbers. So, one country and one list of distinct code values. [HELP]
import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize
df = pd.read_json('projects.json')
df_adj = df[['country','projects']]
for list_entry in df_adj['projects']:
print(list_entry)
OUTPUT:
['code': '8', 'name': 'Human development', 'code': '11', 'name': '']
['code': '1', 'name': 'Economic management', 'code': '6', 'name': 'Social protection and risk management']
['code': '5', 'name': 'Trade and integration', 'code': '2', 'name': 'Public sector governance', 'code': '11', 'name': 'Environment and natural resources management', 'code': '6', 'name': 'Social protection and risk management']
['code': '7', 'name': 'Social dev/gender/inclusion', 'code': '7', 'name': 'Social dev/gender/inclusion']
['code': '5', 'name': 'Trade and integration', 'code': '4', 'name': 'Financial and private sector development']
['code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '4', 'name': 'Financial and private sector development']
['code': '11', 'name': 'Environment and natural resources management', 'code': '8', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '7', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance']
['code': '10', 'name': 'Rural development', 'code': '2', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': 'Social protection and risk management', 'code': '11', 'name': 'Environment and natural resources management']
python python-3.x
add a comment |
I am trying to work with two columns in a dataframe created from a JSON file. One column contains strings of country names, the other column contains a list of dictionaries. Each dictionary represents a given economic or socioeconomic project. Each list varies in size. Each dictionary contains two keys, 'code' and 'name', and there exists only one value for each key. The value for code is a string number i.e. '8' and the value for name is a string title i.e. 'human development'.
So, each country in the country column can appear more than once and with a list of dictionaries in the projects column. Most likely, there are redundant dictionaries. I would like to parse the information into two dataframes.
First, I would like to bag all the code values in a list so that when a country name appears in the country column, there is a list of code values right beside it in the projects column. When I am done gathering all the code values, I will have to count the number of distinct code values that correspond to a given country.
One dataframe should not have its dimensions altered. It should have the name of a country in the country column and the projects column should have a list of string numbers from the code key (already contained in the list of dictionaries for that element), instead of a list of dictionaries. So, one list of code values per country, even if the country appears more than once.[HELP]
The other dataframe will have its dimensionality altered. It will have all the DISTINCT code values collected in a list inside the projects column for a given country in the country column, instead of several appearances of a given country with potentially redundant elements from its list of string numbers. So, one country and one list of distinct code values. [HELP]
import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize
df = pd.read_json('projects.json')
df_adj = df[['country','projects']]
for list_entry in df_adj['projects']:
print(list_entry)
OUTPUT:
['code': '8', 'name': 'Human development', 'code': '11', 'name': '']
['code': '1', 'name': 'Economic management', 'code': '6', 'name': 'Social protection and risk management']
['code': '5', 'name': 'Trade and integration', 'code': '2', 'name': 'Public sector governance', 'code': '11', 'name': 'Environment and natural resources management', 'code': '6', 'name': 'Social protection and risk management']
['code': '7', 'name': 'Social dev/gender/inclusion', 'code': '7', 'name': 'Social dev/gender/inclusion']
['code': '5', 'name': 'Trade and integration', 'code': '4', 'name': 'Financial and private sector development']
['code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '4', 'name': 'Financial and private sector development']
['code': '11', 'name': 'Environment and natural resources management', 'code': '8', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '7', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance']
['code': '10', 'name': 'Rural development', 'code': '2', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': 'Social protection and risk management', 'code': '11', 'name': 'Environment and natural resources management']
python python-3.x
add a comment |
I am trying to work with two columns in a dataframe created from a JSON file. One column contains strings of country names, the other column contains a list of dictionaries. Each dictionary represents a given economic or socioeconomic project. Each list varies in size. Each dictionary contains two keys, 'code' and 'name', and there exists only one value for each key. The value for code is a string number i.e. '8' and the value for name is a string title i.e. 'human development'.
So, each country in the country column can appear more than once and with a list of dictionaries in the projects column. Most likely, there are redundant dictionaries. I would like to parse the information into two dataframes.
First, I would like to bag all the code values in a list so that when a country name appears in the country column, there is a list of code values right beside it in the projects column. When I am done gathering all the code values, I will have to count the number of distinct code values that correspond to a given country.
One dataframe should not have its dimensions altered. It should have the name of a country in the country column and the projects column should have a list of string numbers from the code key (already contained in the list of dictionaries for that element), instead of a list of dictionaries. So, one list of code values per country, even if the country appears more than once.[HELP]
The other dataframe will have its dimensionality altered. It will have all the DISTINCT code values collected in a list inside the projects column for a given country in the country column, instead of several appearances of a given country with potentially redundant elements from its list of string numbers. So, one country and one list of distinct code values. [HELP]
import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize
df = pd.read_json('projects.json')
df_adj = df[['country','projects']]
for list_entry in df_adj['projects']:
print(list_entry)
OUTPUT:
['code': '8', 'name': 'Human development', 'code': '11', 'name': '']
['code': '1', 'name': 'Economic management', 'code': '6', 'name': 'Social protection and risk management']
['code': '5', 'name': 'Trade and integration', 'code': '2', 'name': 'Public sector governance', 'code': '11', 'name': 'Environment and natural resources management', 'code': '6', 'name': 'Social protection and risk management']
['code': '7', 'name': 'Social dev/gender/inclusion', 'code': '7', 'name': 'Social dev/gender/inclusion']
['code': '5', 'name': 'Trade and integration', 'code': '4', 'name': 'Financial and private sector development']
['code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '4', 'name': 'Financial and private sector development']
['code': '11', 'name': 'Environment and natural resources management', 'code': '8', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '7', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance']
['code': '10', 'name': 'Rural development', 'code': '2', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': 'Social protection and risk management', 'code': '11', 'name': 'Environment and natural resources management']
python python-3.x
I am trying to work with two columns in a dataframe created from a JSON file. One column contains strings of country names, the other column contains a list of dictionaries. Each dictionary represents a given economic or socioeconomic project. Each list varies in size. Each dictionary contains two keys, 'code' and 'name', and there exists only one value for each key. The value for code is a string number i.e. '8' and the value for name is a string title i.e. 'human development'.
So, each country in the country column can appear more than once and with a list of dictionaries in the projects column. Most likely, there are redundant dictionaries. I would like to parse the information into two dataframes.
First, I would like to bag all the code values in a list so that when a country name appears in the country column, there is a list of code values right beside it in the projects column. When I am done gathering all the code values, I will have to count the number of distinct code values that correspond to a given country.
One dataframe should not have its dimensions altered. It should have the name of a country in the country column and the projects column should have a list of string numbers from the code key (already contained in the list of dictionaries for that element), instead of a list of dictionaries. So, one list of code values per country, even if the country appears more than once.[HELP]
The other dataframe will have its dimensionality altered. It will have all the DISTINCT code values collected in a list inside the projects column for a given country in the country column, instead of several appearances of a given country with potentially redundant elements from its list of string numbers. So, one country and one list of distinct code values. [HELP]
import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize
df = pd.read_json('projects.json')
df_adj = df[['country','projects']]
for list_entry in df_adj['projects']:
print(list_entry)
OUTPUT:
['code': '8', 'name': 'Human development', 'code': '11', 'name': '']
['code': '1', 'name': 'Economic management', 'code': '6', 'name': 'Social protection and risk management']
['code': '5', 'name': 'Trade and integration', 'code': '2', 'name': 'Public sector governance', 'code': '11', 'name': 'Environment and natural resources management', 'code': '6', 'name': 'Social protection and risk management']
['code': '7', 'name': 'Social dev/gender/inclusion', 'code': '7', 'name': 'Social dev/gender/inclusion']
['code': '5', 'name': 'Trade and integration', 'code': '4', 'name': 'Financial and private sector development']
['code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '4', 'name': 'Financial and private sector development']
['code': '11', 'name': 'Environment and natural resources management', 'code': '8', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '7', 'name': '']
['code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance', 'code': '2', 'name': 'Public sector governance']
['code': '10', 'name': 'Rural development', 'code': '2', 'name': '']
['code': '10', 'name': 'Rural development', 'code': '6', 'name': 'Social protection and risk management', 'code': '6', 'name': 'Social protection and risk management', 'code': '11', 'name': 'Environment and natural resources management']
python python-3.x
python python-3.x
edited Mar 26 at 19:30
Carlos Rivas
asked Mar 26 at 2:38
Carlos RivasCarlos Rivas
14 bronze badges
14 bronze badges
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349078%2fmapping-keys-from-a-list-of-nested-dictionaries-to-columns-in-a-dataframe-from-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349078%2fmapping-keys-from-a-list-of-nested-dictionaries-to-columns-in-a-dataframe-from-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown