33 questions
1
vote
0
answers
74
views
Group arrays with same size efficiently
I am trying to group arrays with the same size.
The arrays can be grouped if all of the values (30) within the array are the same, so all values must be the same.
For example: -1.345509 == -1.345509 ...
2
votes
1
answer
62
views
get group of sublists that share common elements but those elements are not the same in all sublists
I have to group sub-lists with a common element in set "p", using python. p elements are not always in 1st or las position on the sub-list. Grouped (result) lists is list r.
Result list r is ...
-1
votes
1
answer
17
views
self._grouper of itertools.groupby return empty list after cast to dict [duplicate]
Hi I have problem with my program, the value of groupby obj can not be get after casting to dict object.
from itertools import groupby
raw = [
{"name": "A", "age"...
3
votes
4
answers
175
views
List of tuples: combine two dataframe columns if first tuple elements match
I have two separate lists each of n elements, with one being ID numbers and the second being pandas dataframes. I will define them as id and dfs. The dataframes in dfs have the same format with ...
1
vote
1
answer
208
views
Create or use a function to find the repeated sequence of items in a list
I am trying to write a function that takes a list/array and finds the repeated sequence of numbers.
Examples:
[111, 0, 3, 1, 111, 0, 3, 1, 111, 0, 3, 1]
[111, 0, 3, 1] is the block that is being ...
0
votes
3
answers
105
views
returning min from dict values where there are more than one zeros
I have a weird situation with a dict which looks like so:
a={
"key1":[{"a":10,"b":10,"d":0},{"a":100,"b":100,"d":1},{"a&...
-3
votes
1
answer
63
views
Title: Python itertools.groupby gives inconsistent counts when printing only the counts
I'm using Python's itertools.groupby to count consecutive occurrences of elements as mentioned below
from itertools import groupby
data = [1, 1, 3, 2, 3, 3, 4, 5, 5]
sorted_data = sorted(data)
groups ...
1
vote
2
answers
98
views
Groupby using a sequence and computing sum
I've a dataframe like this,
Name COST Timestamp
0 c 8 2023-09-20 15:14:46
1 a 8 2023-09-20 15:14:48
2 c 9 2023-09-20 15:14:55
3 b 10 2023-09-20 15:15:00
4 c 4 2023-...
0
votes
2
answers
93
views
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(...
0
votes
1
answer
68
views
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')))
...
-1
votes
2
answers
64
views
How to group or batch the list of elements based on indices sublist?
How to group or batch the list of elements based on indices sublist ?
Below are the possible instances should be considered. Hope that helps better.
1st Instance : Length of elements == sum of all ...
0
votes
1
answer
112
views
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 ...
-1
votes
1
answer
240
views
How to print list of objects grouped by itertools' groupby?
I have a class with some fields. I created a list of objects of this class and now I want to group the list by one of the fields inside the objects. It almost works, but unfortunately it shows me only ...
0
votes
3
answers
54
views
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', '...
1
vote
1
answer
48
views
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 ...
0
votes
2
answers
231
views
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'},
{'...
2
votes
2
answers
200
views
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'), ...
0
votes
1
answer
208
views
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 = ...
0
votes
1
answer
166
views
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(...
0
votes
1
answer
276
views
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: ...
0
votes
2
answers
68
views
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 ...
-3
votes
2
answers
728
views
Python - Group by any matching value
I have a data structure like this
[
(123, 321),
(123, 456),
(999, 654),
(111, 456),
(999, 898),
(111, 654),
(481, 739),
]
How can I group the tuples together by any matching element? i....
-1
votes
2
answers
733
views
List of strings I want to group together if they contain a specific substring from a master list
I have list of strings that I want to group together if they contain a specific substring from a master list.
Example Input:
["Audi_G71Q3E5T7_Coolant", "Volt_Battery_G9A2B4C6D8E", &...
0
votes
1
answer
1k
views
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 ...
1
vote
0
answers
110
views
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:...
0
votes
1
answer
62
views
Python: Value disappears from the list when using groupby and converting to a dictionary
I am trying to write a simple function that gives the result of a runoff vote.
I start with a nested list with names of candidates, and I would like to group them by the first element and put that ...
0
votes
2
answers
1k
views
How to filter a collection of objects by field value?
How in Python to organize and filter a collection of objects by a field value? I need to filter by being equal to an exact value and by being less than a value.
And how to do it effectively? If I ...
0
votes
2
answers
78
views
Groupby a list of dicts with multiple keys doesn't work even if I use sorted before
I have a list of dictionaries like that :
a = [
{'user_id':'111','clean_label':'VIR SEPA'},
{'user_id':'112','clean_label':'VIR SEPA'},
{'user_id':'111','clean_label':'VIR SEPA'},
]
and I ...
0
votes
1
answer
74
views
Python increment rows starting from a month on month
How to obtain the below result
Current Month is the column which is to be calculated. We need to get the increment every month starting from Jan-18 for every account id.
Every Account First row/ ...
-1
votes
2
answers
104
views
Python - itertools.groupby 2
Just having trouble with itertools.groupby. Given a list of dictionaries,
my_list= [
"AD01", "AD01AA", "AD01AB", "AD01AC", "AD01AD","AD02", ...
2
votes
2
answers
2k
views
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': '...
0
votes
1
answer
53
views
grouping multiple items in list into multiple groups of different size python
I am trying to group .xls files in list infiles based on strings in the .xls file names.
The file names are formatted like this "type_d_cross_profile_glacier_name_A-Z" where type_d is a type ...
1
vote
4
answers
326
views
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'),
("...