How to use 'group by 2 columns' in Python

Every line of 'group by 2 columns' 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
284def groupTable(table,
285 group_column=0,
286 group_function=min,
287 missing_value="na"):
288 '''group table by *group_column*.
289
290 The table need not be sorted.
291 Arguments
292 ---------
293 table : list
294 List of rows
295 group_column : int
296 Column to group on
297 group_function : function
298 Function to apply on grouped values
299 missing_value : string
300 String to use for missing values.
301 '''
302
303 table.sort(lambda x, y: cmp(x[group_column], y[group_column]))
304
305 rows = []
306 last_value = None
307 new_table = []
308
309 for row in table:
310 if row[group_column] != last_value:
311
312 if last_value is not None:
313 new_table.append(
314 __DoGroup(rows, group_column, group_function,
315 missing_value))
316
317 rows = []
318 last_value = row[group_column]
319
320 rows.append(row)
321
322 if last_value is not None:
323 new_table.append(
324 __DoGroup(rows, group_column, group_function, missing_value))
325
326 return new_table

Related snippets