GroupBy results to dictionary of lists
You could groupby
on Column1
and then take Column3
to apply(list)
and call to_dict
?
In [81]: df.groupby('Column1')['Column3'].apply(list).to_dict()
Out[81]: {0: [1], 1: [2, 3, 5], 2: [1, 2], 3: [4, 5], 4: [1], 5: [1, 2, 3]}
Or, do
In [433]: {k: list(v) for k, v in df.groupby('Column1')['Column3']}
Out[433]: {0: [1], 1: [2, 3, 5], 2: [1, 2], 3: [4, 5], 4: [1], 5: [1, 2, 3]}
Pandas groupby and get dict in list
You can use itertuples and defulatdict:
itertuples returns named tuples to iterate over dataframe:
for row in df.itertuples():
print(row)
Pandas(Index=0, x=1, y=3, label=1.0)
Pandas(Index=1, x=4, y=2, label=1.0)
Pandas(Index=2, x=5, y=5, label=2.0)
So taking advantage of this:
from collections import defaultdict
dictionary = defaultdict(list)
for row in df.itertuples():
dummy['x'] = row.x
dummy['y'] = row.y
dummy['index'] = row.Index
dictionary[row.label].append(dummy)
dict(dictionary)
> {1.0: [{'x': 1, 'y': 3, 'index': 0}, {'x': 4, 'y': 2, 'index': 1}],
2.0: [{'x': 5, 'y': 5, 'index': 2}]}
In Python Creating dictionary from the group-by result and making list for the unique elements
Group, apply list, turn into dict.
result = df.groupby(['pid', 'wips', 'be'])['rownum'].apply(list).reset_index().to_dict(orient='records')
Convert result from groupby on multiple columns to list of dictionaries of dictionaries
Let us try defining a function dictify
that takes the input argument as the multiindex pandas series and return the nested dictionary in the required format
def dictify(s):
if s.index.nlevels == 1: return s.to_dict()
return {k: dictify(g.droplevel(0)) for k, g in s.groupby(level=0)}
records = [{k: v} for k, v in dictify(df).items()]
>>> df
DE SW Tech
2021-01-01 s1 t1 1
2021-04-02 s1 t1 699
t2 268
2021-04-06 s1 t1 2
s2 t1 466
t2 81
t3 954
Name: COUNT(*), Length: 474, dtype: int64
>>> records
[{'2021-01-01': {'s1': {'t1': 1}}},
{'2021-04-02': {'s1': {'t1': 699, 't2': 268}}},
{'2021-04-06': {'s1': {'t1': 2}, 's2': {'t1': 466, 't2': 81, 't3': 954}}}]
Convert pandas.groupby to dict
You can use dict
with tuple
/ list
applied on your groupby
:
res = dict(tuple(d.groupby('a')))
A memory efficient alternative to dict
is to create a groupby
object and then use get_group
:
res = d.groupby('a')
res.get_group(1) # select dataframe where column 'a' = 1
In cases where the resulting table requires a minor manipulation, like resetting the index, or removing the groupby column, continue to use a dictionary comprehension.
res = {k: v.drop('a', axis=1).reset_index(drop=True) for k, v in d.groupby('a')}
How to convert pandas groupby result into a consolidated dictionary?
Try adding this code using reindex
, repeat
, reset_index
, get_dummies
, groupby
and to_dict
:
groupings = groupings.reset_index()
groupings.columns = [i if i != 0 else 'Total' for i in groupings.columns]
groupings = groupings.reindex(groupings.index.repeat(groupings['Total'])).reset_index(drop=True)
groupings['Total'] = 1
print(pd.get_dummies(groupings, columns=['Player', 'Status'], prefix='', prefix_sep='').groupby(['Country', 'State', 'Team'], as_index=False).sum().to_dict('records'))
Output:
[{'Country': 'Australia', 'State': 'Victoria', 'Team': 'United', 'Total': 4, 'Davis': 0, 'Dudley': 0, 'James': 0, 'Jones': 1, 'Kumar': 0, 'Singh': 0, 'Smith': 3, 'NBA': 1, 'NBA Legend': 0, 'Normal': 3}, {'Country': 'India', 'State': 'Karnataka', 'Team': 'Beasts', 'Total': 1, 'Davis': 0, 'Dudley': 0, 'James': 0, 'Jones': 0, 'Kumar': 1, 'Singh': 0, 'Smith': 0, 'NBA': 0, 'NBA Legend': 0, 'Normal': 1}, {'Country': 'India', 'State': 'Punjab', 'Team': 'Steelers', 'Total': 1, 'Davis': 0, 'Dudley': 0, 'James': 0, 'Jones': 0, 'Kumar': 0, 'Singh': 1, 'Smith': 0, 'NBA': 0, 'NBA Legend': 0, 'Normal': 1}, {'Country': 'United States', 'State': 'California', 'Team': 'Lakers', 'Total': 2, 'Davis': 1, 'Dudley': 0, 'James': 1, 'Jones': 0, 'Kumar': 0, 'Singh': 0, 'Smith': 0, 'NBA': 1, 'NBA Legend': 1, 'Normal': 0}, {'Country': 'United States', 'State': 'New York', 'Team': 'Globetrotters', 'Total': 1, 'Davis': 0, 'Dudley': 1, 'James': 0, 'Jones': 0, 'Kumar': 0, 'Singh': 0, 'Smith': 0, 'NBA': 0, 'NBA Legend': 0, 'Normal': 1}]
Pandas DataFrame.groupby() to dictionary with multiple columns for value
Customize the function you use in apply
so it returns a list of lists for each group:
df.groupby('Column1')[['Column2', 'Column3']].apply(lambda g: g.values.tolist()).to_dict()
# {0: [[23, 1]],
# 1: [[5, 2], [2, 3], [19, 5]],
# 2: [[56, 1], [22, 2]],
# 3: [[2, 4], [14, 5]],
# 4: [[59, 1]],
# 5: [[44, 1], [1, 2], [87, 3]]}
If you need a list of tuples explicitly, use list(map(tuple, ...))
to convert:
df.groupby('Column1')[['Column2', 'Column3']].apply(lambda g: list(map(tuple, g.values.tolist()))).to_dict()
# {0: [(23, 1)],
# 1: [(5, 2), (2, 3), (19, 5)],
# 2: [(56, 1), (22, 2)],
# 3: [(2, 4), (14, 5)],
# 4: [(59, 1)],
# 5: [(44, 1), (1, 2), (87, 3)]}
python : group by multiple dictionary keys
You can use groupby()
to group dicts in combination with itemgetter()
:
from itertools import groupby
from operator import itemgetter
list_pts = [
{'city': 'Madrid', 'year': '2017', 'date': '05/07/2017', 'pts': 7},
{'city': 'Madrid', 'year': '2017', 'date': '14/11/2017', 'pts': 5},
{'city': 'Londres', 'year': '2018', 'date': '25/02/2018', 'pts': 5},
{'city': 'Paris', 'year': '2019', 'date': '17/04/2019', 'pts' : 4},
{'city': 'Londres', 'year': '2019', 'date': '15/06/2019', 'pts': 8},
{'city': 'Paris', 'year': '2019', 'date': '21/08/2019', 'pts': 8},
{'city': 'Londres', 'year': '2019', 'date': '04/12/2019', 'pts': 2}
]
city_year_getter = itemgetter('city', 'year')
date_pts_getter = itemgetter('date', 'pts')
result = []
for (city, year), objs in groupby(sorted(list_pts, key=city_year_getter),
city_year_getter):
dates, ptss = zip(*map(date_pts_getter, objs))
result.append({
'city': city,
'year': year,
'date': list(dates),
'pts': list(ptss)
})
Related Topics
Split a String by Spaces -- Preserving Quoted Substrings -- in Python
Adding Days to a Date in Python
How to Round a Number to Significant Figures in Python
How to Select Rows in a Dataframe Between Two Values, in Python Pandas
Generating Permutations with Repetitions
In Pandas, Is Inplace = True Considered Harmful, or Not
Python3 --Version Shows "Nameerror: Name 'Python3' Is Not Defined"
How to Upgrade All Python Packages with Pip
How to Install Python3 Version of Package via Pip on Ubuntu
How to Calculate the Date Six Months from the Current Date Using the Datetime Python Module
How to Use Python to Login to a Webpage and Retrieve Cookies for Later Usage
Pandas Read_Csv: Low_Memory and Dtype Options
Python List VS. Array - When to Use
Keep Only Date Part When Using Pandas.To_Datetime