Rename a column that is just after a specific one

Hi,

I import some extract from a SAP Frontend into Knime and for some column the “ID” is in a column and the “Text/Description” in the column just after.
The ID column has the right column header, but the “Text/Desciption” doesn’t have any header (if you open the excel file, the header is merged on the two column).

I have several column like these, and I’m wondering how you would solve it.
The new column name could be the same as ID one with “+ Description” for example. Change by position is not possible because column are not always at the same position.

I was thinking about a “Column Expression Node” that would check column header 1 by 1 and if the name start with “Column_*” then take the name of predeccessor colum and add " Description" as new column header but I don’t think we can rename header with this node.

Maybe Javascript node, but I don’t know Javascript at all…

Hi @Zarkoff95

What really helps to speed up the response from forum members is to upload a sample dataset. How does your input looks like and what is the desired output.

gr, Hans

3 Likes

Hi @HansS

Sure, you’re right.

here is what extract would look like in excel

and after a loading in knime, you’ll notice empty_B and empty_F

that I would want to rename them respectively as “Reference Description” and “Familly Decription”.

Obviously this example is super simplified.
Reference or Familly could be in other position next time (so I can’t just rely on
empty_B = Reference Description… empty_B could even not exist next run)

But a WF that could detect if column header starts with “empty_”, and if yes, see the previous column header name “Reference”, add a suffix to it " Decription" and column B header would become “Reference Description”, otherwise keep the column header. (columns in that case will always be contiguous)

See attached a very simple excel file with 2 tabs, run1 and run2 in which I just inverted the column order to simulate the fact that the column are not always at the same position

example - Copy.xlsx (12.1 KB)

2 Likes

Hi @Zarkoff95 ,

Try this

It fetches the column names for the existing table, determines the name of the previous column and if the current column name is “empty_*”, it creates an updated name which can then be used by Column Renamer dictionary to rename those columns

Rename specific columns based on previous column name.knwf (85.7 KB)

4 Likes

@takbb
Thank you very much, it works like a charm.
I had to adpat the “Row Filter” node because I’m stuck to version 5.2 in my compagny (even for update of nodes), but I could manage it with your explanation.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.