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
Why were contact sensors put on three of the Lunar Module's four legs? Did they ever bend and stick out sideways?
Desktop app status bar: Notification vs error message
Summoning A Technology Based Demon
Is it safe if the neutral lead is exposed and disconnected?
Should I accept an invitation to give a talk from someone who might review my proposal?
How did the Axis intend to hold the Caucasus?
Copying an existing HTML page and use it, is that against any copyright law?
Can a US President, after impeachment and removal, be re-elected or re-appointed?
Incrementing add under condition in pandas
What language is Raven using for her attack in the new 52?
Telling manager project isn't worth the effort?
This day in history III
Is it error of law to judge on less relevant case law when there is much more relevant one?
Why would anyone ever invest in a cash-only etf?
Reading line from terminal in expl3
Polyhedra, Polyhedron, Polytopes and Polygon
Does Dispel Magic destroy Artificer Turrets?
Why is it considered acid rain with pH <5.6?
Dobbs Murder Mystery : A Picture worth 1000 words?
Why did Windows 95 crash the whole system but newer Windows only crashed programs?
Can I change the license of a forked project to the MIT if the license of the parent project has changed from the GPL to the MIT?
Anti-cheating: should there be a limit to a number of toilet breaks per game per player?
How long until two planets become one?
Golden Guardian removed before death related trigger
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