Quantcast
Channel: pandas - create key value pair from grouped by data frame - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by AChampion for pandas - create key value pair from grouped by data frame

$
0
0

You can iterate over the MultiIndex series:

>>> s = df.set_index(['ColX', 'ColY'])['Sum']>>> {k: v.reset_index(level=0, drop=True).to_dict() for k, v in s.groupby(level=0)}{'A': {'a': 3, 'b': 2, 'c': 1}, 'B': {'p': 5, 'q': 6, 'r': 7}}

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>