![]() |
|
[Tut] Python List of Lists Group By – A Simple Illustrated Guide - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python List of Lists Group By – A Simple Illustrated Guide (/thread-94990.html) |
[Tut] Python List of Lists Group By – A Simple Illustrated Guide - xSicKxBot - 05-11-2020 Python List of Lists Group By – A Simple Illustrated Guide This tutorial shows you how to group the inner listsof a Python list of lists by common element. There are three basic methods:
Before we explore these three options in more detail, let’s give you the quick solution first using the Pandas library in our interactive shell: https://repl.it/@finxter/pandaslistoflistsgroupby?lite=true You can run this code in your browser. If you want to learn about the Pythonicalternatives or you need a few more explanations, then read on! Method 1: Group List of Lists By Common Element in Dictionary Problem: Given a list of lists. Group the elements by common element and store the result in a dictionary(key = common element). ![]() Example: Say, you’ve got a database with multiple rows (the list of lists) where each row consists of three attributes: Name, Age, and Income. You want to group by Name and store the result in a dictionary. The dictionary keys are given by the Name attribute. The dictionary values are a list of rows that have this exact Name attribute. Solution: Here’s the data and how you can group by a common attribute (e.g., Name). Code: # Database:
# row = [Name, Age, Income]
rows = [['Alice', 19, 45000], ['Bob', 18, 22000], ['Ann', 26, 88000], ['Alice', 33, 118000]] # Create a dictionary grouped by Name
d = {}
for row in rows: # Add name to dict if not exists if row[0] not in d: d[row[0]] = [] # Add all non-Name attributes as a new list d[row[0]].append(row[1:]) print(d)
# {'Alice': [[19, 45000], [33, 118000]],
# 'Bob': [[18, 22000]],
# 'Ann': [[26, 88000]]}You can see that the result is a dictionary with one key per name ( Code: 'Alice'Code: 'Bob'Code: 'Ann'The strategy how you accomplish this is simple:
So far, so good. But what if you want to perform some aggregation on the grouped database rows? Method 2: Group List of Lists By Common Element and Aggregate Grouped Elements Problem: In the previous example, you’ve seen that each dictionary value is a list of lists because you store each row as a separate list. But what if you want to aggregate all grouped rows? Example: The dictionary entry for the key Code: 'Alice'Code: [[19, 45000], [33, 118000]]Code: [(19+33)/2, (45000+118000)/2]Solution: The solution is simply to add one post-processing step after the above code to aggregate all attributes using the Code: zip()Code: # Database:
# row = [Name, Age, Income]
rows = [['Alice', 19, 45000], ['Bob', 18, 22000], ['Ann', 26, 88000], ['Alice', 33, 118000]] # Create a dictionary grouped by Name
d = {}
for row in rows: # Add name to dict if not exists if row[0] not in d: d[row[0]] = [] # Add all non-Name attributes as a new list d[row[0]].append(row[1:]) print(d)
# {'Alice': [[19, 45000], [33, 118000]],
# 'Bob': [[18, 22000]],
# 'Ann': [[26, 88000]]} # AGGREGATION FUNCTION:
for key in d: d[key] = [sum(x) / len(x) for x in zip(*d[key])] print(d)
# {'Alice': [26.0, 81500.0], 'Bob': [18.0, 22000.0], 'Ann': [26.0, 88000.0]}In the code, you use the aggregation function Code: sum(x) / len(x)Explanation:
See what happens in this code snippet in this interactive memory visualization tool (by clicking “Next”): https://pythontutor.com/iframe-embed.html#code=%23%20Database%3A%0A%23%20row%20%3D%20%5BName,%20Age,%20Income%5D%0Arows%20%3D%20%5B%5B'Alice',%2019,%2045000%5D,%0A%20%20%20%20%20%20%20%20%5B'Bob',%2018,%2022000%5D,%0A%20%20%20%20%20%20%20%20%5B'Ann',%2026,%2088000%5D,%0A%20%20%20%20%20%20%20%20%5B'Alice',%2033,%20118000%5D%5D%0A%0A%0A%23%20Create%20a%20dictionary%20grouped%20by%20Name%0Ad%20%3D%20%7B%7D%0Afor%20row%20in%20rows%3A%0A%0A%20%20%20%20%23%20Add%20name%20to%20dict%20if%20not%20exists%0A%20%20%20%20if%20row%5B0%5D%20not%20in%20d%3A%0A%20%20%20%20%20%20%20%20d%5Brow%5B0%5D%5D%20%3D%20%5B%5D%0A%0A%20%20%20%20%23%20Add%20all%20non-Name%20attributes%20as%20a%20new%20list%0A%20%20%20%20d%5Brow%5B0%5D%5D.append%28row%5B1%3A%5D%29%0A%0A%23%20AGGREGATION%20FUNCTION%3A%0Afor%20key%20in%20d%3A%0A%20%20%20%20d%5Bkey%5D%20%3D%20%5Bsum%28x%29%20/%20len%28x%29%20for%20x%20in%20zip%28*d%5Bkey%5D%29%5D%0A%0Aprint%28d%29%0A&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false Method 3: Pandas GroupBy The Pandas library has its own powerful implementation of the groupby() function. Have a look at the code first: Code: # Database:
# row = [Name, Age, Income]
rows = [['Alice', 19, 45000], ['Bob', 18, 22000], ['Ann', 26, 88000], ['Alice', 33, 118000]] import pandas as pd
df = pd.DataFrame(rows) print(df) ''' 0 1 2
0 Alice 19 45000
1 Bob 18 22000
2 Ann 26 88000
3 Alice 33 118000 ''' print(df.groupby([0]).mean()) ''' 1 2
0 Alice 26 81500
Ann 26 88000
Bob 18 22000 '''Explanation:
Where to Go From Here? Enough theory, let’s get some practice! To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs? Practice projects is how you sharpen your saw in coding! Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people? Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner. Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |