Is this a homework problem?
In a real-life application, I would consider the following first:
- choose another datastructure for buses, eg a
structor aNamedTuple, - don’t convert to strings for keys, use
idirectly, - wrap the whole thing in a function, and just pass
data["bus"]to it, - think about returning the whole bus datastructure instead of
key(but this depends on the context) - use a standard idiom like
findallorfilterfor getting buses of a given type (are they unique by type?)
See the manual for all of these.
An alternative is just dumping everything in a dataframe using a “tidy” structure. See