#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Cumulative Sum preparation for graphing?


    Hello,

    I have a geodatabase table that is something like:

    Object Year Acres
    1 1980 5
    2 1979 3
    3 1978 2
    4 1980 8
    5 1979 2
    6 1976 6

    I would like to use python to create a cumulative sum by year so that I end up with a table that looks like:
    1976 6
    1978 2
    1979 5
    1980 13

    I'm going to be graphing this using matplotlib, showing trends in acres over time.

    I'm a beginner with python. So far I've tried to use FeatureClasstoNumPyArray (because my data are in a ArcGIS feature class dataset) and numpy's cumsum, but I can't get the array structured correctly to sum by year. I think part of my problem is that I don't really understand how a table gets converted to an array.

    If numpy is the wrong approach, pleases let me know. My requirements are that I can graph cumulative acres over time from a dataset that has polygons of various sizes occuring in specific years. If it matters for processing time, this dataset has about 140,000 records. Also, if you have any suggestions for python graphing modules or sample code that will do with kind of thing well, I'd like to know about them.

    thanks!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Code:
    # numpy is terrific, but dictionaries are right for this application.
    
    import collections
    
    data = '''
    Object Year Acres
    1 1980 5
    2 1979 3
    3 1978 2
    4 1980 8
    5 1979 2
    6 1976 6
    '''
    
    D = collections.defaultdict(list)
    
    for L in data.split('\n'):
        if L and (L[0] in '0123456789'):
            FIELDS = L.split()
            if 3 == len(FIELDS):
                D[FIELDS[1]].append(int(FIELDS[-1]))
    
    for key in sorted(D):
        print('%s %d'%(key,sum(D[key])))
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo