I'm trying to flatten a table, but I want to keep/relate certain fields tied to each other. For context, I ran a Generate Near Table to try to find the traffic volumes near intersections.

I am trying to keep the "Route_Name" field tied to the "Curr_Vol" field, while flattening it to the input field "IN_FID". (IN_FID is intersections, so we need to know what traffic volumes are near the intersection, and what route name is related to each traffic volume)

I've tried doing a pivot table, but I can't flatten the table to IN_FID completely while keeping the Route_Name and Curr_Vol fields.

Original data: enter image description here

After trying to run Pivot Table:

enter image description here

What data needs to look like in the end: enter image description here

The result I want is a table where each row will have; The unique IN_FID value, the Route_Name & Curr_Vol values from every row in the original table, arranged as name1, vol1, name2, vol2, and so on.

I'm wondering if an ArcPy script would be the best solution?

However, I don't know how to write that complex of code.

1 Answer 1


If you are willing to use the simple PyShp library, this solution should work...

import shapefile
from collections import defaultdict

f = r'path/to/shapefile.shp'

feat_dict = defaultdict(list)

with shapefile.Reader(f) as shp:

    for rec in shp.iterRecords():
        # By using default dict, you can append to the existing dictionary list

for k, v in feat_dict.items():
    print(k, v)

You could also easily write that to a csv file if needed.

PyShp can be installed just by running "pip install pyshp" in your command line.

Not the answer you're looking for? Browse other questions tagged or ask your own question.