Merge two list of dicts with same key











up vote
7
down vote

favorite
1












I have two lists of dictionaries. Both lists always have the same ids.
I want to get the following result:



{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


The following code is working but I wonder if this is the best solution?



l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]

l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]


l3 = {x['id']: {'av': x['av']} for x in l1}
l4 = {x['id']: {'nv': x['nv']} for x in l2}

{key: value.update(l4[key]) for key, value in l3.items()}

>> {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}









share|improve this question









New contributor




sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    l1 and l2 have their id in the same order, is it always the case?
    – Mathias Ettinger
    Dec 7 at 10:51










  • The order is not always the same
    – sascha
    Dec 7 at 10:56















up vote
7
down vote

favorite
1












I have two lists of dictionaries. Both lists always have the same ids.
I want to get the following result:



{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


The following code is working but I wonder if this is the best solution?



l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]

l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]


l3 = {x['id']: {'av': x['av']} for x in l1}
l4 = {x['id']: {'nv': x['nv']} for x in l2}

{key: value.update(l4[key]) for key, value in l3.items()}

>> {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}









share|improve this question









New contributor




sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    l1 and l2 have their id in the same order, is it always the case?
    – Mathias Ettinger
    Dec 7 at 10:51










  • The order is not always the same
    – sascha
    Dec 7 at 10:56













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





I have two lists of dictionaries. Both lists always have the same ids.
I want to get the following result:



{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


The following code is working but I wonder if this is the best solution?



l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]

l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]


l3 = {x['id']: {'av': x['av']} for x in l1}
l4 = {x['id']: {'nv': x['nv']} for x in l2}

{key: value.update(l4[key]) for key, value in l3.items()}

>> {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}









share|improve this question









New contributor




sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have two lists of dictionaries. Both lists always have the same ids.
I want to get the following result:



{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


The following code is working but I wonder if this is the best solution?



l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]

l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]


l3 = {x['id']: {'av': x['av']} for x in l1}
l4 = {x['id']: {'nv': x['nv']} for x in l2}

{key: value.update(l4[key]) for key, value in l3.items()}

>> {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}






python dictionary






share|improve this question









New contributor




sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Dec 7 at 13:55









200_success

128k15149412




128k15149412






New contributor




sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 7 at 10:34









sascha

1444




1444




New contributor




sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






sascha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    l1 and l2 have their id in the same order, is it always the case?
    – Mathias Ettinger
    Dec 7 at 10:51










  • The order is not always the same
    – sascha
    Dec 7 at 10:56














  • 1




    l1 and l2 have their id in the same order, is it always the case?
    – Mathias Ettinger
    Dec 7 at 10:51










  • The order is not always the same
    – sascha
    Dec 7 at 10:56








1




1




l1 and l2 have their id in the same order, is it always the case?
– Mathias Ettinger
Dec 7 at 10:51




l1 and l2 have their id in the same order, is it always the case?
– Mathias Ettinger
Dec 7 at 10:51












The order is not always the same
– sascha
Dec 7 at 10:56




The order is not always the same
– sascha
Dec 7 at 10:56










2 Answers
2






active

oldest

votes

















up vote
8
down vote



accepted










Your approach is rather good but your implementation is hardly extensible.



For starter, you don't need l4 as you can update l3 directly instead:



l3 = {x['id']: {'av': x['av']} for x in l1}
for d in l2:
l3[d['id']].update(nv=d['nv'])


Second, you can pop the id so you don't have to know the other keys in the various dictionaries:



l3 = {d.pop('id'): d for d in l1}
for d in l2:
l3[d.pop('id')].update(d)


But this approach has the drawback of modifying all input dictionaries. To mitigate that, we can start with an empty dictionary, update with every keys (including id) and pop the extra key afterwards. This is easily done using a defaultdict:



from collections import defaultdict


result = defaultdict(dict)
for sequence in (l1, l2):
for dictionary in sequence:
result[dictionary['id']].update(dictionary)
for dictionary in result.values():
dictionary.pop('id')


There are few overheads using this approach compared to the first version, but it is way easier to generalize so you are able to merge more than 2 lists. Speaking of which, in such case, you should define a function taking a variable number of lists to merge:



import itertools
from collections import defaultdict


def merge(shared_key, *iterables)
result = defaultdict(dict)
for dictionary in itertools.chain.from_iterable(iterables):
result[dictionary[shared_key]].update(dictionary)
for dictionary in result.values():
dictionary.pop(shared_key)
return result


Usage be like



merge('id', l1, l2)





share|improve this answer























  • Great solution, thanks for the detailed answer.
    – sascha
    Dec 7 at 12:02


















up vote
6
down vote













If you want to merge lists of dicts, you don't have to reinvent the wheel.



pandas might be a 800-pound gorilla but it's included in many distros, is well tested and documented.



You just need to initialize the dataframes, set their index and merge them:



import pandas as pd

l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]

df1 = pd.DataFrame(l1).set_index('id')
df2 = pd.DataFrame(l2).set_index('id')
df = df1.merge(df2, left_index=True, right_index=True)
df.T.to_dict()
# {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


Here's the corresponding console output:



>>> l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
>>> l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]
>>> import pandas as pd
>>> df1 = pd.DataFrame(l1).set_index('id')
>>> df1
av
id
9 4
10 0
8 0
>>> df2 = pd.DataFrame(l2).set_index('id')
>>> df2
nv
id
9 45
10 0
8 30
>>> df = df1.merge(df2, left_index=True, right_index=True)
>>> df
av nv
id
9 4 45
10 0 0
8 0 30
>>> df.T.to_dict()
{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}





share|improve this answer

















  • 2




    Heavy handed, but nice solution.
    – Steve
    Dec 7 at 16:22











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

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: "196"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});






sascha is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209202%2fmerge-two-list-of-dicts-with-same-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
8
down vote



accepted










Your approach is rather good but your implementation is hardly extensible.



For starter, you don't need l4 as you can update l3 directly instead:



l3 = {x['id']: {'av': x['av']} for x in l1}
for d in l2:
l3[d['id']].update(nv=d['nv'])


Second, you can pop the id so you don't have to know the other keys in the various dictionaries:



l3 = {d.pop('id'): d for d in l1}
for d in l2:
l3[d.pop('id')].update(d)


But this approach has the drawback of modifying all input dictionaries. To mitigate that, we can start with an empty dictionary, update with every keys (including id) and pop the extra key afterwards. This is easily done using a defaultdict:



from collections import defaultdict


result = defaultdict(dict)
for sequence in (l1, l2):
for dictionary in sequence:
result[dictionary['id']].update(dictionary)
for dictionary in result.values():
dictionary.pop('id')


There are few overheads using this approach compared to the first version, but it is way easier to generalize so you are able to merge more than 2 lists. Speaking of which, in such case, you should define a function taking a variable number of lists to merge:



import itertools
from collections import defaultdict


def merge(shared_key, *iterables)
result = defaultdict(dict)
for dictionary in itertools.chain.from_iterable(iterables):
result[dictionary[shared_key]].update(dictionary)
for dictionary in result.values():
dictionary.pop(shared_key)
return result


Usage be like



merge('id', l1, l2)





share|improve this answer























  • Great solution, thanks for the detailed answer.
    – sascha
    Dec 7 at 12:02















up vote
8
down vote



accepted










Your approach is rather good but your implementation is hardly extensible.



For starter, you don't need l4 as you can update l3 directly instead:



l3 = {x['id']: {'av': x['av']} for x in l1}
for d in l2:
l3[d['id']].update(nv=d['nv'])


Second, you can pop the id so you don't have to know the other keys in the various dictionaries:



l3 = {d.pop('id'): d for d in l1}
for d in l2:
l3[d.pop('id')].update(d)


But this approach has the drawback of modifying all input dictionaries. To mitigate that, we can start with an empty dictionary, update with every keys (including id) and pop the extra key afterwards. This is easily done using a defaultdict:



from collections import defaultdict


result = defaultdict(dict)
for sequence in (l1, l2):
for dictionary in sequence:
result[dictionary['id']].update(dictionary)
for dictionary in result.values():
dictionary.pop('id')


There are few overheads using this approach compared to the first version, but it is way easier to generalize so you are able to merge more than 2 lists. Speaking of which, in such case, you should define a function taking a variable number of lists to merge:



import itertools
from collections import defaultdict


def merge(shared_key, *iterables)
result = defaultdict(dict)
for dictionary in itertools.chain.from_iterable(iterables):
result[dictionary[shared_key]].update(dictionary)
for dictionary in result.values():
dictionary.pop(shared_key)
return result


Usage be like



merge('id', l1, l2)





share|improve this answer























  • Great solution, thanks for the detailed answer.
    – sascha
    Dec 7 at 12:02













up vote
8
down vote



accepted







up vote
8
down vote



accepted






Your approach is rather good but your implementation is hardly extensible.



For starter, you don't need l4 as you can update l3 directly instead:



l3 = {x['id']: {'av': x['av']} for x in l1}
for d in l2:
l3[d['id']].update(nv=d['nv'])


Second, you can pop the id so you don't have to know the other keys in the various dictionaries:



l3 = {d.pop('id'): d for d in l1}
for d in l2:
l3[d.pop('id')].update(d)


But this approach has the drawback of modifying all input dictionaries. To mitigate that, we can start with an empty dictionary, update with every keys (including id) and pop the extra key afterwards. This is easily done using a defaultdict:



from collections import defaultdict


result = defaultdict(dict)
for sequence in (l1, l2):
for dictionary in sequence:
result[dictionary['id']].update(dictionary)
for dictionary in result.values():
dictionary.pop('id')


There are few overheads using this approach compared to the first version, but it is way easier to generalize so you are able to merge more than 2 lists. Speaking of which, in such case, you should define a function taking a variable number of lists to merge:



import itertools
from collections import defaultdict


def merge(shared_key, *iterables)
result = defaultdict(dict)
for dictionary in itertools.chain.from_iterable(iterables):
result[dictionary[shared_key]].update(dictionary)
for dictionary in result.values():
dictionary.pop(shared_key)
return result


Usage be like



merge('id', l1, l2)





share|improve this answer














Your approach is rather good but your implementation is hardly extensible.



For starter, you don't need l4 as you can update l3 directly instead:



l3 = {x['id']: {'av': x['av']} for x in l1}
for d in l2:
l3[d['id']].update(nv=d['nv'])


Second, you can pop the id so you don't have to know the other keys in the various dictionaries:



l3 = {d.pop('id'): d for d in l1}
for d in l2:
l3[d.pop('id')].update(d)


But this approach has the drawback of modifying all input dictionaries. To mitigate that, we can start with an empty dictionary, update with every keys (including id) and pop the extra key afterwards. This is easily done using a defaultdict:



from collections import defaultdict


result = defaultdict(dict)
for sequence in (l1, l2):
for dictionary in sequence:
result[dictionary['id']].update(dictionary)
for dictionary in result.values():
dictionary.pop('id')


There are few overheads using this approach compared to the first version, but it is way easier to generalize so you are able to merge more than 2 lists. Speaking of which, in such case, you should define a function taking a variable number of lists to merge:



import itertools
from collections import defaultdict


def merge(shared_key, *iterables)
result = defaultdict(dict)
for dictionary in itertools.chain.from_iterable(iterables):
result[dictionary[shared_key]].update(dictionary)
for dictionary in result.values():
dictionary.pop(shared_key)
return result


Usage be like



merge('id', l1, l2)






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 7 at 12:36









Toby Speight

23.2k638110




23.2k638110










answered Dec 7 at 11:50









Mathias Ettinger

23.1k33179




23.1k33179












  • Great solution, thanks for the detailed answer.
    – sascha
    Dec 7 at 12:02


















  • Great solution, thanks for the detailed answer.
    – sascha
    Dec 7 at 12:02
















Great solution, thanks for the detailed answer.
– sascha
Dec 7 at 12:02




Great solution, thanks for the detailed answer.
– sascha
Dec 7 at 12:02












up vote
6
down vote













If you want to merge lists of dicts, you don't have to reinvent the wheel.



pandas might be a 800-pound gorilla but it's included in many distros, is well tested and documented.



You just need to initialize the dataframes, set their index and merge them:



import pandas as pd

l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]

df1 = pd.DataFrame(l1).set_index('id')
df2 = pd.DataFrame(l2).set_index('id')
df = df1.merge(df2, left_index=True, right_index=True)
df.T.to_dict()
# {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


Here's the corresponding console output:



>>> l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
>>> l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]
>>> import pandas as pd
>>> df1 = pd.DataFrame(l1).set_index('id')
>>> df1
av
id
9 4
10 0
8 0
>>> df2 = pd.DataFrame(l2).set_index('id')
>>> df2
nv
id
9 45
10 0
8 30
>>> df = df1.merge(df2, left_index=True, right_index=True)
>>> df
av nv
id
9 4 45
10 0 0
8 0 30
>>> df.T.to_dict()
{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}





share|improve this answer

















  • 2




    Heavy handed, but nice solution.
    – Steve
    Dec 7 at 16:22















up vote
6
down vote













If you want to merge lists of dicts, you don't have to reinvent the wheel.



pandas might be a 800-pound gorilla but it's included in many distros, is well tested and documented.



You just need to initialize the dataframes, set their index and merge them:



import pandas as pd

l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]

df1 = pd.DataFrame(l1).set_index('id')
df2 = pd.DataFrame(l2).set_index('id')
df = df1.merge(df2, left_index=True, right_index=True)
df.T.to_dict()
# {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


Here's the corresponding console output:



>>> l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
>>> l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]
>>> import pandas as pd
>>> df1 = pd.DataFrame(l1).set_index('id')
>>> df1
av
id
9 4
10 0
8 0
>>> df2 = pd.DataFrame(l2).set_index('id')
>>> df2
nv
id
9 45
10 0
8 30
>>> df = df1.merge(df2, left_index=True, right_index=True)
>>> df
av nv
id
9 4 45
10 0 0
8 0 30
>>> df.T.to_dict()
{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}





share|improve this answer

















  • 2




    Heavy handed, but nice solution.
    – Steve
    Dec 7 at 16:22













up vote
6
down vote










up vote
6
down vote









If you want to merge lists of dicts, you don't have to reinvent the wheel.



pandas might be a 800-pound gorilla but it's included in many distros, is well tested and documented.



You just need to initialize the dataframes, set their index and merge them:



import pandas as pd

l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]

df1 = pd.DataFrame(l1).set_index('id')
df2 = pd.DataFrame(l2).set_index('id')
df = df1.merge(df2, left_index=True, right_index=True)
df.T.to_dict()
# {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


Here's the corresponding console output:



>>> l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
>>> l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]
>>> import pandas as pd
>>> df1 = pd.DataFrame(l1).set_index('id')
>>> df1
av
id
9 4
10 0
8 0
>>> df2 = pd.DataFrame(l2).set_index('id')
>>> df2
nv
id
9 45
10 0
8 30
>>> df = df1.merge(df2, left_index=True, right_index=True)
>>> df
av nv
id
9 4 45
10 0 0
8 0 30
>>> df.T.to_dict()
{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}





share|improve this answer












If you want to merge lists of dicts, you don't have to reinvent the wheel.



pandas might be a 800-pound gorilla but it's included in many distros, is well tested and documented.



You just need to initialize the dataframes, set their index and merge them:



import pandas as pd

l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]

df1 = pd.DataFrame(l1).set_index('id')
df2 = pd.DataFrame(l2).set_index('id')
df = df1.merge(df2, left_index=True, right_index=True)
df.T.to_dict()
# {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}


Here's the corresponding console output:



>>> l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
>>> l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]
>>> import pandas as pd
>>> df1 = pd.DataFrame(l1).set_index('id')
>>> df1
av
id
9 4
10 0
8 0
>>> df2 = pd.DataFrame(l2).set_index('id')
>>> df2
nv
id
9 45
10 0
8 30
>>> df = df1.merge(df2, left_index=True, right_index=True)
>>> df
av nv
id
9 4 45
10 0 0
8 0 30
>>> df.T.to_dict()
{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 7 at 15:11









Eric Duminil

2,0261613




2,0261613








  • 2




    Heavy handed, but nice solution.
    – Steve
    Dec 7 at 16:22














  • 2




    Heavy handed, but nice solution.
    – Steve
    Dec 7 at 16:22








2




2




Heavy handed, but nice solution.
– Steve
Dec 7 at 16:22




Heavy handed, but nice solution.
– Steve
Dec 7 at 16:22










sascha is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















sascha is a new contributor. Be nice, and check out our Code of Conduct.













sascha is a new contributor. Be nice, and check out our Code of Conduct.












sascha is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • 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.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209202%2fmerge-two-list-of-dicts-with-same-key%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Category:香港粉麵

List *all* the tuples!

Channel [V]