Skip to main content
Solved

change format of table

  • February 27, 2018
  • 3 replies
  • 12 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.

View original
Did this help you find an answer to your question?

3 replies

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • 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
Contributor
Forum|alt.badge.img+19
  • Contributor
  • February 28, 2018
takashi wrote:

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
takashi wrote:

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.

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings