Skip to main content
Solved

change format of table

  • February 27, 2018
  • 3 replies
  • 26 views

barrett_h
Contributor
Forum|alt.badge.img+1

Hi all,

I have an attribute table in the following format:

localitycountmonthbright123janbright456febbright789marmyrtleford987janmyrtleford654febmyrtleford321mar

I would like to change it to the following format:

localityjanfebmarbright123456789myrtleford987654321

I have a convoluted process using an attribute filter and a bunch of statistics calculators but it's not very scaleable. I was wondering if anyone can point me in the right direction for an improved solution!

Cheers,

Barrett

Best answer by takashi

Hi @barrett_h, I think a general approach is to rename 'count' to the value of 'month' in the same record, and then aggregate the records grouped by 'locality'. An implementation example is:

Instead of renaming with the BulkAttributeRenamer, you can also use the AttributeCreator to create a new attribute whose name is equal to the value of 'month' and assign the value of 'count' to that. This may be easier to understand.

Here is another approach. Create a JSON document containing pairs of key (month) and value (count), and flatten it to extract the key/value pairs as attributes.

There is more than one way, but I usually use the BulkAttributeRenamer in a similar case.

Hope this helps.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

takashi
Celebrity
  • Best Answer
  • February 28, 2018

Hi @barrett_h, I think a general approach is to rename 'count' to the value of 'month' in the same record, and then aggregate the records grouped by 'locality'. An implementation example is:

Instead of renaming with the BulkAttributeRenamer, you can also use the AttributeCreator to create a new attribute whose name is equal to the value of 'month' and assign the value of 'count' to that. This may be easier to understand.

Here is another approach. Create a JSON document containing pairs of key (month) and value (count), and flatten it to extract the key/value pairs as attributes.

There is more than one way, but I usually use the BulkAttributeRenamer in a similar case.

Hope this helps.


takashi
Celebrity
  • February 28, 2018

Hi @barrett_h, I think a general approach is to rename 'count' to the value of 'month' in the same record, and then aggregate the records grouped by 'locality'. An implementation example is:

0684Q00000ArKSAQA3.png

Instead of renaming with the BulkAttributeRenamer, you can also use the AttributeCreator to create a new attribute whose name is equal to the value of 'month' and assign the value of 'count' to that. This may be easier to understand.

0684Q00000ArKPGQA3.png

Here is another approach. Create a JSON document containing pairs of key (month) and value (count), and flatten it to extract the key/value pairs as attributes.

0684Q00000ArKdqQAF.png

There is more than one way, but I usually use the BulkAttributeRenamer in a similar case.

Hope this helps.

If you were familiar with Python scripting, the PythonCaller could also be an alternative of course.

 

# PythonCaller Script Example
class FeatureProcessor(object):
    def __init__(self):
        # {key=locaity : value=feature}
        self.kv = {}
        
    def input(self, feature):
        loc = feature.getAttribute('locality')
        mon = feature.getAttribute('month')
        cnt = feature.getAttribute('count')
        self.kv.setdefault(loc, feature).setAttribute(mon, cnt)
        
    def close(self):
        for feature in self.kv.values():
            self.pyoutput(feature)
0684Q00000ArMK4QAN.png

 


barrett_h
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • February 28, 2018

Hi @barrett_h, I think a general approach is to rename 'count' to the value of 'month' in the same record, and then aggregate the records grouped by 'locality'. An implementation example is:

Instead of renaming with the BulkAttributeRenamer, you can also use the AttributeCreator to create a new attribute whose name is equal to the value of 'month' and assign the value of 'count' to that. This may be easier to understand.

Here is another approach. Create a JSON document containing pairs of key (month) and value (count), and flatten it to extract the key/value pairs as attributes.

There is more than one way, but I usually use the BulkAttributeRenamer in a similar case.

Hope this helps.

Those are great suggestions @takashi! Thank you very much, I ended up going for the JSON idea as it provides so much flexibility.