Questions tagged [itertools-groupby]

Function in the itertools module. Makes an iterator that returns **consecutive keys** and groups from the iterable (note the difference from similarly named GROUPBY in SQL, pandas etc. which groups rows by the same values). Be sure to include the `python` tag for increased visibility.

groupby method in the itertools module makes an iterator that returns groups from an iterable.

Unlike the similarly named GROUPBY in SQL databases or data manipulation packages such as (which group by common elements regardless of order), itertools.groupby() creates a new group every time the value of the key changes. For example, [1,1,2,1] is three groups: [[1,1], [2], [1]]. To mimic the GROUPBY in SQL, it is often needed to sort the input by the keys to be grouped on.

Reference / related tags:

25 questions
2
votes
2 answers

How do I group by with name, preserve keys and names using itertools?

I am working with a data set that is a simple SQL Query that fetches the desired rows. [(2, 5, 'JOHN K', 'YAHOO'), (2, 6, 'AARON M', 'YAHOO'), (2, 7, 'NICK C', 'YAHOO'), (1, 2, 'CELESTE G', 'GOOGLE'), (1, 3, 'RICH M', 'GOOGLE'), (1, 4, 'SANDEEP C',…
ThinkCode
  • 7,841
  • 21
  • 73
  • 92
2
votes
2 answers

Python itertools.groupby with dictionaries with multiple values

I am trying to use the Python itertools.groupby function to change this list: items = [ {'price': 5.0, 'name': 'Strawberries'}, {'price': 5.0, 'name': 'Strawberries'}, {'price': 5.0, 'name': 'Strawberries'}, {'price': 11.23, 'name':…
1
vote
3 answers

Sum categories by unique values in list in python

I have this list: [('2023-03-15', 'paris', 'flight', 4), ('2023-03-21', 'berlin', 'flight', 2), ('2023-03-01', 'madrid', 'drive', 10), ('2023-03-04', 'madrid', 'cycling', 3), ('2023-03-08', 'rome', 'train', 9), ('2023-03-11', 'amsterdam', 'flight',…
Zen4ttitude
  • 143
  • 10
1
vote
1 answer

Can itertools.groupby use pd.NA?

I tried using itertools.groupby with a pandas Series. But I got: TypeError: boolean value of NA is ambiguous Indeed some of my values are NA. This is a minimal reproducible example: import pandas as pd import itertools g =…
Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74
1
vote
0 answers

How to iterate over nested groups in pandas dataframe and assign values to a new column?

I have a data frame: Id modification_date date1 date2 estimate_date aaa 2022-07-19T13:27:01Z 2022-07-21T20:01:00Z 2022-07-21T22:59:00Z aaa 2022-07-20T13:49:21Z 2022-07-21T20:01:00Z 2022-07-21T22:59:00Z …
RCN
  • 101
  • 10
1
vote
4 answers

Given a large array of tuples, how to groupby the first element of each tuple in order to sum the last element of each tuple without Pandas dataframe?

I have a large list of tuples where each tuple contains 9 string elements: pdf_results = [ ("Kohl's - Dallas", '-', "Kohl's Cafe", '03/18/22', 'RC', '8', '0', '16', '8'), ("Kohl's - Dallas", '-', "Kohl's Cafe", '03/18/22', 'SMI', '5', '0', '10',…
0
votes
2 answers

How to group two element tuples for a continuous range of first element with same second element?

I have a list of two element tuples, the first element of each tuple is an integer, these tuples are equivalent to key-value pairs, in fact these tuples are flattened dict_items, generated using list(d.items()). The element in the first position is…
Ξένη Γήινος
  • 2,181
  • 1
  • 9
  • 35
0
votes
1 answer

How to get corresponding values of other column using idxmax in aggregation

Following is the code weekly = (df.groupby(['Year','Qtr_Year'], as_index=False).agg(High=('High', 'max'),D_HIGH='High','idxmax'))) I want to use idxmax and find…
0
votes
1 answer

How do I use groupby on the output of a mapper?

This is a continuation of my previous question: How to print only if a character is an alphabet? I now have a mapper that is working perfectly, and it's giving me this output when I use a text file with the string `It's a beautiful life". i 1 1 0 t…
Zaku
  • 180
  • 6
0
votes
2 answers

Grouping Python dictionaries in hierarchical form with multiple keys?

Here is my list of dicts: [{'subtopic': 'kuku', 'topic': 'lulu', 'attachments': ['ttt'], 'text': 'abc'}, {'subtopic': 'tutu', 'topic': 'lulu', 'attachments': ['pipu'], 'text': 'bubb'}, {'subtopic': 'did', 'topic': 'lulu', …
SteveS
  • 3,789
  • 5
  • 30
  • 64
0
votes
1 answer

pandas: list comprehension for creating column in existing dataframe

I have one column, and another is datetime which is in index position I done some coding on this. I try to use for loop for below condition but need optimization or list comprehension. date_today = datetime.now() days = pd.date_range(date_today,…
Sushil Kokil
  • 137
  • 1
  • 1
  • 11
0
votes
1 answer

How to get the number of instances in an itertools._grouper object?

Suppose I have the below dataframe: >>>from itertools import groupby >>>import pandas as pd >>>idx1 = pd.date_range('2019-01-01',periods=5) >>>idx2 = pd.date_range('2020-06-01',periods=5) >>>idx3 = pd.date_range('2021-08-15',periods=5) >>>idx4 =…
jgg
  • 791
  • 4
  • 17
0
votes
1 answer

Python itertools.groupby gives unexpected results when sorting by binary representation

I am trying to solve a simple problem using itertools.groupby: group the numbers from 0 to 7 according to the number of 1's in their binary representation. So I want to produce the mapping {0: [0], 1: [1, 2, 4], 2: [3, 5, 6], 3: [7]} But here is…
tBuLi
  • 2,295
  • 2
  • 16
  • 16
0
votes
2 answers

Group emails into TO & CC with itertools.groupby and convert it to a dictionary

I'd like to group emails by their domain and convert the result into a dictionary. So far I have figured out that itertools.groupby with a custom func will do that. It correctly assigns keys to each value, but when I try to create a dictionary only…
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
0
votes
1 answer

Rust: Itertools::GroupBy and ownership

I am a newbie in Rust, and I am having trouble understanding Itertools::GroupBy. Here is an example I came up with to practice GroupBy. Given an array of integers: Group it based on the value of the numbers. Keep only groups with more than 3…
Lukas
  • 27
  • 7
1
2