Python for Scouting and Data Analysis
Session 08
Advanced Data Structures
Issaquah Robotics Society
FRC 1318
Session 8 Topics
Session 8 Overview
Reading List
List Comprehensions
# Open JSON file
with open("districts.json", "r") as file:
dist = json.load(file)
# Simple list comprehension
[x["abbreviation"] for x in dist]
# More complex list comprehension (get
[x["abbreviation"] for x in dist if len(x["abbreviation"])==2]
git checkout master
git pull origin master
git checkout <your_branch_name>
git rebase master
Dictionary Comprehensions
# Dictionary Comprehension
{x["team_key"]: x["point_total"] for x in dist_rankings if x["rank"] <= 20}
Comprehension Exercises
Why Use List or Dictionary Comprehensions?
Numpy
Numpy Exercises
Ordered Dictionaries
Exercises
unordered_dict = {"1": "a", "2": "b", "3": "c"}
for key, val in unordered_dict.items():
print(key, val)
# Will output be 1, 2, 3? 3, 1, 2? No way to know.
Look!� A tuple!
Pandas Dataframes
Pandas converted each dictionary to a row and each dictionary key to a column
JSON TEXT (dist)
[{'abbreviation': 'chs',� 'display_name': 'FIRST Chesapeake',� 'key': '2017chs',� 'year': 2017},� {'abbreviation': 'fim',� 'display_name': 'FIRST In Michigan',� 'key': '2017fim',� 'year': 2017},� {'abbreviation': 'in',� 'display_name': 'Indiana FIRST',� 'key': '2017in',� 'year': 2017},
…
]�
import pandas as pd
# Open JSON file
with open("districts.json", "r") as file:
dist = json.load(file)
df = pd.DataFrame(dist)
Pandas DataFrame (as displayed in Jupyter)
Anatomy of a Dataframe
Column
Row Index
Column Index
# Select single column
df["display_name"]
# Select several rows
df[2:4]
# Select portion
df.loc[7:9, "abbreviation":"key"]
# Select portion with integers
df.iloc[0:3, 0:1]
Row
DataFrame Exercises�(Flat Data)
DataFrame Exercises (Nested Data)
| event_points | point_total | rank | rookie_bonus | team_key |
0 | [{'alliance_points': 16, 'award_points': 5, 'd... | 349 | 1 | 0 | frc2471 |
1 | [{'alliance_points': 15, 'award_points': 10, '... | 344 | 2 | 0 | frc2557 |
2 | [{'alliance_points': 16, 'award_points': 5, 'd... | 317 | 3 | 0 | frc1425 |
Nested and Normalized Data
[{'event_points':
[{'alliance_points': 16,� 'award_points': 5,� 'district_cmp': False,� 'elim_points': 30,� 'event_key': '2018orwil',� 'qual_points': 18,� 'total': 69},� {'alliance_points': 16,� 'award_points': 5,� 'district_cmp': False,� 'elim_points': 30,� 'event_key': '2018orlak',� 'qual_points': 22,� 'total': 73},� {'alliance_points': 48,� 'award_points': 15,� 'district_cmp': True,� 'elim_points': 90,� 'event_key': '2018pncmp',� 'qual_points': 54,� 'total': 207}],� 'point_total': 349,� 'rank': 1,� 'rookie_bonus': 0,� 'team_key': 'frc2471'},�
Normalizing Dataframe
from pandas.io.json import json_normalize
json_normalize(dist_rankings, record_path="event_data",
meta=["point_total", "rank",
"rookie_bonus", "team_key"])
Questions
Minimize the Cost, Maximize the Value!
Change Log
Date | Description of Change | Originator |
30 Oct 2018 | Added change log | S. Irwin |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |