rename many similar attribute

Sadegh95
Sadegh95 New Altair Community Member
edited November 5 in Community Q&A
hi there. i have so many attributes that named "att_1, att_2, ..., att_500". i want to rename them to eg "attribute". how can i do that? Thanks

Best Answer

  • CKönig
    CKönig New Altair Community Member
    edited October 2023 Answer ✓
    Hi @Sadegh95
    You are most likely looking for the De-Pivot operator:



    Here is the example process I created showing you both the preferred De-Pivot version as well as a manual version making use of Loop Attributes that you were asking for originally. Please remember, that looping over data is usually much slower than specialized operators.
    <?xml version="1.0" encoding="UTF-8"?><process version="10.2.000">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="10.2.000" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" breakpoints="after" class="subprocess" compatibility="10.2.000" expanded="true" height="82" name="Subprocess" width="90" x="45" y="34">
            <process expanded="true">
              <operator activated="true" class="retrieve" compatibility="10.2.000" expanded="true" height="68" name="Retrieve Iris" width="90" x="45" y="34">
                <parameter key="repository_entry" value="//Samples/data/Iris"/>
              </operator>
              <operator activated="true" class="blending:select_attributes" compatibility="10.2.000" expanded="true" height="82" name="Select Attributes" width="90" x="179" y="34">
                <parameter key="type" value="exclude attributes"/>
                <parameter key="attribute_filter_type" value="one attribute"/>
                <parameter key="select_attribute" value="label"/>
                <parameter key="select_subset" value="id␞label"/>
                <parameter key="also_apply_to_special_attributes_(id,_label..)" value="true"/>
              </operator>
              <operator activated="true" class="rename_by_replacing" compatibility="10.2.000" expanded="true" height="82" name="Rename by Replacing" width="90" x="313" y="34">
                <parameter key="attribute_filter_type" value="regular_expression"/>
                <parameter key="attribute" value=""/>
                <parameter key="attributes" value=""/>
                <parameter key="regular_expression" value="a[0-9]"/>
                <parameter key="use_except_expression" value="false"/>
                <parameter key="value_type" value="attribute_value"/>
                <parameter key="use_value_type_exception" value="false"/>
                <parameter key="except_value_type" value="time"/>
                <parameter key="block_type" value="attribute_block"/>
                <parameter key="use_block_type_exception" value="false"/>
                <parameter key="except_block_type" value="value_matrix_row_start"/>
                <parameter key="invert_selection" value="false"/>
                <parameter key="include_special_attributes" value="false"/>
                <parameter key="replace_what" value="a([0-9])"/>
                <parameter key="replace_by" value="Person$1"/>
              </operator>
              <connect from_op="Retrieve Iris" from_port="output" to_op="Select Attributes" to_port="example set input"/>
              <connect from_op="Select Attributes" from_port="example set output" to_op="Rename by Replacing" to_port="example set input"/>
              <connect from_op="Rename by Replacing" from_port="example set output" to_port="out 1"/>
              <portSpacing port="source_in 1" spacing="0"/>
              <portSpacing port="sink_out 1" spacing="0"/>
              <portSpacing port="sink_out 2" spacing="0"/>
            </process>
            <description align="center" color="transparent" colored="false" width="126">generate example data</description>
          </operator>
          <operator activated="true" class="multiply" compatibility="10.2.000" expanded="true" height="103" name="Multiply" width="90" x="179" y="34"/>
          <operator activated="true" class="de_pivot" compatibility="10.2.000" expanded="true" height="82" name="De-Pivot" width="90" x="380" y="34">
            <list key="attribute_name">
              <parameter key="Person" value="Person[0-9]*"/>
            </list>
            <parameter key="index_attribute" value="OriginalName"/>
            <parameter key="create_nominal_index" value="true"/>
            <parameter key="keep_missings" value="false"/>
            <description align="center" color="transparent" colored="false" width="126">Transform a wide ExampleSet with multiple Attributes measuring the same thing into a long ExampleSet with one Attribute</description>
          </operator>
          <operator activated="true" class="concurrency:loop_attributes" compatibility="10.2.000" expanded="true" height="82" name="Loop Attributes" width="90" x="313" y="340">
            <parameter key="attribute_filter_type" value="regular_expression"/>
            <parameter key="attribute" value=""/>
            <parameter key="attributes" value=""/>
            <parameter key="regular_expression" value="Person[0-9]*"/>
            <parameter key="use_except_expression" value="false"/>
            <parameter key="value_type" value="attribute_value"/>
            <parameter key="use_value_type_exception" value="false"/>
            <parameter key="except_value_type" value="time"/>
            <parameter key="block_type" value="attribute_block"/>
            <parameter key="use_block_type_exception" value="false"/>
            <parameter key="except_block_type" value="value_matrix_row_start"/>
            <parameter key="invert_selection" value="false"/>
            <parameter key="include_special_attributes" value="false"/>
            <parameter key="attribute_name_macro" value="loop_attribute"/>
            <parameter key="reuse_results" value="false"/>
            <parameter key="enable_parallel_execution" value="true"/>
            <process expanded="true">
              <operator activated="true" class="blending:select_attributes" compatibility="10.2.000" expanded="true" height="82" name="Select Attributes (3)" width="90" x="179" y="34">
                <parameter key="type" value="include attributes"/>
                <parameter key="attribute_filter_type" value="one attribute"/>
                <parameter key="select_attribute" value="%{loop_attribute}"/>
                <parameter key="select_subset" value=""/>
                <parameter key="also_apply_to_special_attributes_(id,_label..)" value="false"/>
                <description align="center" color="transparent" colored="false" width="126">only keep the current attribute</description>
              </operator>
              <operator activated="true" class="blending:rename" compatibility="10.2.000" expanded="true" height="82" name="Rename" width="90" x="313" y="34">
                <list key="rename attributes">
                  <parameter key="%{loop_attribute}" value="Person"/>
                </list>
                <parameter key="from_attribute" value=""/>
                <parameter key="to_attribute" value=""/>
                <description align="center" color="transparent" colored="false" width="126">rename it to &amp;quot;Person&amp;quot;</description>
              </operator>
              <connect from_port="input 1" to_op="Select Attributes (3)" to_port="example set input"/>
              <connect from_op="Select Attributes (3)" from_port="example set output" to_op="Rename" to_port="example set input"/>
              <connect from_op="Rename" from_port="example set output" to_port="output 1"/>
              <portSpacing port="source_input 1" spacing="0"/>
              <portSpacing port="source_input 2" spacing="0"/>
              <portSpacing port="sink_output 1" spacing="0"/>
              <portSpacing port="sink_output 2" spacing="0"/>
              <description align="center" color="yellow" colored="false" height="105" resized="false" width="113" x="28" y="69">The complete ExampleSet is delivered at the input port</description>
            </process>
            <description align="center" color="transparent" colored="false" width="126">Loop over all &amp;quot;Person&amp;quot; attributes&lt;br/&gt;&lt;br/&gt;the current attribute name is stored in a macro</description>
          </operator>
          <operator activated="true" class="append" compatibility="10.2.000" expanded="true" height="82" name="Append" width="90" x="581" y="340">
            <parameter key="datamanagement" value="double_array"/>
            <parameter key="data_management" value="auto"/>
            <parameter key="merge_type" value="all"/>
            <description align="center" color="transparent" colored="false" width="126">flatten the collection by appending everything&lt;br/&gt;&lt;br/&gt;This works, because all ExampleSets have the same shape</description>
          </operator>
          <connect from_op="Subprocess" from_port="out 1" to_op="Multiply" to_port="input"/>
          <connect from_op="Multiply" from_port="output 1" to_op="De-Pivot" to_port="example set input"/>
          <connect from_op="Multiply" from_port="output 2" to_op="Loop Attributes" to_port="input 1"/>
          <connect from_op="De-Pivot" from_port="example set output" to_port="result 1"/>
          <connect from_op="Loop Attributes" from_port="output 1" to_op="Append" to_port="example set 1"/>
          <connect from_op="Append" from_port="merged set" to_port="result 2"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="231"/>
          <portSpacing port="sink_result 3" spacing="0"/>
          <description align="center" color="yellow" colored="false" height="143" resized="false" width="141" x="423" y="384">Result will be a collection of individual ExampleSets each containing one original &amp;quot;column&amp;quot; renamed to Person</description>
          <description align="center" color="yellow" colored="false" height="132" resized="true" width="290" x="674" y="96">Both ExampleSets will contain the same values, but in a different order:&lt;br/&gt;&lt;br/&gt;De-Pivot works row-wise&lt;br/&gt;&lt;br/&gt;Loop Attributes works column-wise</description>
        </process>
      </operator>
    </process>
    
    Hope this helps!

    Kind regards,
    Christian

Answers

  • MartinLiebig
    MartinLiebig
    Altair Employee
    Hey,
    have a look at Rename by Replacing. Note that column names need to be unique.

    Cheers,
    Martin
  • Sadegh95
    Sadegh95 New Altair Community Member
    I found a way in topics (now couldn't found it) that replace the attributes by combining loop attributes and rename by replacing which can replace attributes to a specific name in one column. i want that way or similar way.
    thanks for your regard
  • MartinLiebig
    MartinLiebig
    Altair Employee
    Sorry, but I do not understand the task at hand.
  • Sadegh95
    Sadegh95 New Altair Community Member
    sorry
    i want to rename many columns. can you give me the regular expression to change my attribute names by "rename by replacing" operators?
  • MartinLiebig
    MartinLiebig
    Altair Employee
    From
    att_1 -> attribute_1
    att_2 -> attribute_2
    etc?
    If so:
    att\_(\d+)
    replacement with
    attribute_$1
    should do it
  • Sadegh95
    Sadegh95 New Altair Community Member
    edited October 2023
    I've returned (:
    thank you very much for your response and for the time you spent. i renamed the columns with that expression. now i want to rename them to a unique name (eg all columns renamed to "Sell"). i can rename by select attributes and rename operator. but my columns are too many. what's the solution? i previously did it, but now i forgot ):

  • CKönig
    CKönig New Altair Community Member
    Hey @Sadegh95,

    as Martin already mentioned: all Attribute (column) names must be unique, duplicate names are not allowed.

    May I ask, why you would want to do that? If the data in those colums indeed represents the same thing, you could think about de-pivoting to create multiple Examples (rows) out of them. You can have a look at the tutorial process for the "De-Pivot" operator to see an example.
  • Sadegh95
    Sadegh95 New Altair Community Member
    thank you but I've did it before with loop operators. now i can't remember. i can do it with the operators, but i want to do it automatically by loop. in the following image i do these actions:
    1) select the attributes Person_1
    2) rename it to Person
    3)select the attributes Person_2
    4) rename it to Person
    5) select the attributes Person_3
    6) rename it to Person
    and so on.
  • CKönig
    CKönig New Altair Community Member
    edited October 2023 Answer ✓
    Hi @Sadegh95
    You are most likely looking for the De-Pivot operator:



    Here is the example process I created showing you both the preferred De-Pivot version as well as a manual version making use of Loop Attributes that you were asking for originally. Please remember, that looping over data is usually much slower than specialized operators.
    <?xml version="1.0" encoding="UTF-8"?><process version="10.2.000">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="10.2.000" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" breakpoints="after" class="subprocess" compatibility="10.2.000" expanded="true" height="82" name="Subprocess" width="90" x="45" y="34">
            <process expanded="true">
              <operator activated="true" class="retrieve" compatibility="10.2.000" expanded="true" height="68" name="Retrieve Iris" width="90" x="45" y="34">
                <parameter key="repository_entry" value="//Samples/data/Iris"/>
              </operator>
              <operator activated="true" class="blending:select_attributes" compatibility="10.2.000" expanded="true" height="82" name="Select Attributes" width="90" x="179" y="34">
                <parameter key="type" value="exclude attributes"/>
                <parameter key="attribute_filter_type" value="one attribute"/>
                <parameter key="select_attribute" value="label"/>
                <parameter key="select_subset" value="id␞label"/>
                <parameter key="also_apply_to_special_attributes_(id,_label..)" value="true"/>
              </operator>
              <operator activated="true" class="rename_by_replacing" compatibility="10.2.000" expanded="true" height="82" name="Rename by Replacing" width="90" x="313" y="34">
                <parameter key="attribute_filter_type" value="regular_expression"/>
                <parameter key="attribute" value=""/>
                <parameter key="attributes" value=""/>
                <parameter key="regular_expression" value="a[0-9]"/>
                <parameter key="use_except_expression" value="false"/>
                <parameter key="value_type" value="attribute_value"/>
                <parameter key="use_value_type_exception" value="false"/>
                <parameter key="except_value_type" value="time"/>
                <parameter key="block_type" value="attribute_block"/>
                <parameter key="use_block_type_exception" value="false"/>
                <parameter key="except_block_type" value="value_matrix_row_start"/>
                <parameter key="invert_selection" value="false"/>
                <parameter key="include_special_attributes" value="false"/>
                <parameter key="replace_what" value="a([0-9])"/>
                <parameter key="replace_by" value="Person$1"/>
              </operator>
              <connect from_op="Retrieve Iris" from_port="output" to_op="Select Attributes" to_port="example set input"/>
              <connect from_op="Select Attributes" from_port="example set output" to_op="Rename by Replacing" to_port="example set input"/>
              <connect from_op="Rename by Replacing" from_port="example set output" to_port="out 1"/>
              <portSpacing port="source_in 1" spacing="0"/>
              <portSpacing port="sink_out 1" spacing="0"/>
              <portSpacing port="sink_out 2" spacing="0"/>
            </process>
            <description align="center" color="transparent" colored="false" width="126">generate example data</description>
          </operator>
          <operator activated="true" class="multiply" compatibility="10.2.000" expanded="true" height="103" name="Multiply" width="90" x="179" y="34"/>
          <operator activated="true" class="de_pivot" compatibility="10.2.000" expanded="true" height="82" name="De-Pivot" width="90" x="380" y="34">
            <list key="attribute_name">
              <parameter key="Person" value="Person[0-9]*"/>
            </list>
            <parameter key="index_attribute" value="OriginalName"/>
            <parameter key="create_nominal_index" value="true"/>
            <parameter key="keep_missings" value="false"/>
            <description align="center" color="transparent" colored="false" width="126">Transform a wide ExampleSet with multiple Attributes measuring the same thing into a long ExampleSet with one Attribute</description>
          </operator>
          <operator activated="true" class="concurrency:loop_attributes" compatibility="10.2.000" expanded="true" height="82" name="Loop Attributes" width="90" x="313" y="340">
            <parameter key="attribute_filter_type" value="regular_expression"/>
            <parameter key="attribute" value=""/>
            <parameter key="attributes" value=""/>
            <parameter key="regular_expression" value="Person[0-9]*"/>
            <parameter key="use_except_expression" value="false"/>
            <parameter key="value_type" value="attribute_value"/>
            <parameter key="use_value_type_exception" value="false"/>
            <parameter key="except_value_type" value="time"/>
            <parameter key="block_type" value="attribute_block"/>
            <parameter key="use_block_type_exception" value="false"/>
            <parameter key="except_block_type" value="value_matrix_row_start"/>
            <parameter key="invert_selection" value="false"/>
            <parameter key="include_special_attributes" value="false"/>
            <parameter key="attribute_name_macro" value="loop_attribute"/>
            <parameter key="reuse_results" value="false"/>
            <parameter key="enable_parallel_execution" value="true"/>
            <process expanded="true">
              <operator activated="true" class="blending:select_attributes" compatibility="10.2.000" expanded="true" height="82" name="Select Attributes (3)" width="90" x="179" y="34">
                <parameter key="type" value="include attributes"/>
                <parameter key="attribute_filter_type" value="one attribute"/>
                <parameter key="select_attribute" value="%{loop_attribute}"/>
                <parameter key="select_subset" value=""/>
                <parameter key="also_apply_to_special_attributes_(id,_label..)" value="false"/>
                <description align="center" color="transparent" colored="false" width="126">only keep the current attribute</description>
              </operator>
              <operator activated="true" class="blending:rename" compatibility="10.2.000" expanded="true" height="82" name="Rename" width="90" x="313" y="34">
                <list key="rename attributes">
                  <parameter key="%{loop_attribute}" value="Person"/>
                </list>
                <parameter key="from_attribute" value=""/>
                <parameter key="to_attribute" value=""/>
                <description align="center" color="transparent" colored="false" width="126">rename it to &amp;quot;Person&amp;quot;</description>
              </operator>
              <connect from_port="input 1" to_op="Select Attributes (3)" to_port="example set input"/>
              <connect from_op="Select Attributes (3)" from_port="example set output" to_op="Rename" to_port="example set input"/>
              <connect from_op="Rename" from_port="example set output" to_port="output 1"/>
              <portSpacing port="source_input 1" spacing="0"/>
              <portSpacing port="source_input 2" spacing="0"/>
              <portSpacing port="sink_output 1" spacing="0"/>
              <portSpacing port="sink_output 2" spacing="0"/>
              <description align="center" color="yellow" colored="false" height="105" resized="false" width="113" x="28" y="69">The complete ExampleSet is delivered at the input port</description>
            </process>
            <description align="center" color="transparent" colored="false" width="126">Loop over all &amp;quot;Person&amp;quot; attributes&lt;br/&gt;&lt;br/&gt;the current attribute name is stored in a macro</description>
          </operator>
          <operator activated="true" class="append" compatibility="10.2.000" expanded="true" height="82" name="Append" width="90" x="581" y="340">
            <parameter key="datamanagement" value="double_array"/>
            <parameter key="data_management" value="auto"/>
            <parameter key="merge_type" value="all"/>
            <description align="center" color="transparent" colored="false" width="126">flatten the collection by appending everything&lt;br/&gt;&lt;br/&gt;This works, because all ExampleSets have the same shape</description>
          </operator>
          <connect from_op="Subprocess" from_port="out 1" to_op="Multiply" to_port="input"/>
          <connect from_op="Multiply" from_port="output 1" to_op="De-Pivot" to_port="example set input"/>
          <connect from_op="Multiply" from_port="output 2" to_op="Loop Attributes" to_port="input 1"/>
          <connect from_op="De-Pivot" from_port="example set output" to_port="result 1"/>
          <connect from_op="Loop Attributes" from_port="output 1" to_op="Append" to_port="example set 1"/>
          <connect from_op="Append" from_port="merged set" to_port="result 2"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="231"/>
          <portSpacing port="sink_result 3" spacing="0"/>
          <description align="center" color="yellow" colored="false" height="143" resized="false" width="141" x="423" y="384">Result will be a collection of individual ExampleSets each containing one original &amp;quot;column&amp;quot; renamed to Person</description>
          <description align="center" color="yellow" colored="false" height="132" resized="true" width="290" x="674" y="96">Both ExampleSets will contain the same values, but in a different order:&lt;br/&gt;&lt;br/&gt;De-Pivot works row-wise&lt;br/&gt;&lt;br/&gt;Loop Attributes works column-wise</description>
        </process>
      </operator>
    </process>
    
    Hope this helps!

    Kind regards,
    Christian
  • Sadegh95
    Sadegh95 New Altair Community Member
    Hi Christian
    Thank you very much. I used the operators.
  • kesarteraa
    kesarteraa New Altair Community Member
    Thank you very much for your response and for the time you spent.