Quantcast
Channel: pandas - create key value pair from grouped by data frame - Stack Overflow
Browsing latest articles
Browse All 4 View Live

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

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':...

View Article


Answer by Allen Qin for pandas - create key value pair from grouped by data...

#A to_dict() solutiond = df.groupby(['Col X','Col Y']).sum().reset_index().pivot(columns='Col X',values='Sum').to_dict()Out[70]: {'A': {0: 3.0, 1: 2.0, 2: 1.0, 3: nan, 4: nan, 5: nan},'B': {0: nan, 1:...

View Article


Answer by piRSquared for pandas - create key value pair from grouped by data...

Use a dictionary comprehension while iterating via a groupby object{name: dict(zip(g['Col Y'], g['Sum'])) for name, g in df.groupby('Col X')}{'A': {'a': 3, 'b': 2, 'c': 1}, 'B': {'p': 5, 'q': 6, 'r':...

View Article

pandas - create key value pair from grouped by data frame

I have a data frame with three columns, I would like to create a dictionary after applying groupby function on first and second column.I can do this by for loops, but is there any pandas way of doing...

View Article
Browsing latest articles
Browse All 4 View Live


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