6 examples of 'pandas groupby iterate' in Python

Every line of 'pandas groupby iterate' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
125def _iter_groups(self, df, y=None):
126 """Iterate over groups of `df`, and, if provided, matching labels."""
127 groups = df.groupby(self.groupby).indices
128 for key, sub_idx in groups.items():
129 sub_df = df.iloc[sub_idx]
130 sub_y = y[sub_idx] if y is not None else None
131 yield key, sub_df, sub_y
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

31def groupby(xs, keys):
32 result = defaultdict(list)
33 for (x, key) in zip(xs, keys):
34 result[key].append(x)
35 return result
204def __getitem__(self, index):
205 return StreamingSeriesGroupby(self.root, self.grouper, index)
2665def __iter__(self):
2666 return iteraggregate(self.source, self.key, self.aggregators, self.errorvalue)
17def groupby_deco(func):
18 def func_wrapper(self, thing, *args, **kwargs):
19 if isinstance(thing, pd.core.groupby.DataFrameGroupBy):
20 agg = thing.apply(lambda x: func(self, x, *args, **kwargs))
21 is_series = isinstance(agg, pd.core.series.Series)
22 has_multiindex = isinstance(agg.index, pd.MultiIndex)
23 if is_series and has_multiindex:
24 return agg.unstack()
25 else:
26 return agg
27 return func(self, thing, *args, **kwargs)
28 return func_wrapper
376def group_by(collection, iteratee=None):
377 """Creates an object composed of keys generated from the results of running
378 each element of a `collection` through the iteratee.
379
380 Args:
381 collection (list|dict): Collection to iterate over.
382 iteratee (mixed, optional): Iteratee applied per iteration.
383
384 Returns:
385 dict: Results of grouping by `iteratee`.
386
387 Example:
388
389 >>> results = group_by([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], 'a')
390 >>> assert results == {1: [{'a': 1, 'b': 2}], 3: [{'a': 3, 'b': 4}]}
391 >>> results = group_by([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], {'a': 1})
392 >>> assert results == {False: [{'a': 3, 'b': 4}],\
393 True: [{'a': 1, 'b': 2}]}
394
395 .. versionadded:: 1.0.0
396 """
397 ret = {}
398 cbk = pyd.iteratee(iteratee)
399
400 for value in collection:
401 key = cbk(value)
402 ret.setdefault(key, [])
403 ret[key].append(value)
404
405 return ret

Related snippets