问题描述:

I have a dataset like this, and I'm looking for a way to add a category, based on what kind of product I have.

Can I search for Apple + Orange and assign them to a category named Fruits, and similar with Milk + Wine and assign them to another category, named Drinks?

| Item | Category |

|-------|----------|

| Apple | | <-- Fruits

| Orange| | <-- Fruits

| Milk | | <-- Drinks

| Wine | | <-- Drinks

Or maybe a simpler method: find any rows containing Milk and assign them to category Drinks?

网友答案:

This is something you can do without code.

  • Filter or facet in the Item field for each value
  • Create a facet on the Category field
  • Click the edit button next to the blank value in the Category facet and type with the category you want to add.
  • Edit your Item facet or filter to move to the next category and repeat this process until you have categorized all yours items.
网友答案:

As magdmartin says you can do this using facets and edits - the solution he describes is probably the simplest approach and least error prone. However, if you prefer to do in a single step you can use GREL to test the content of the Item cell and then set the value in the Category cell dependent on the content of the Item cell.

with(cells["Item"].value.toLowercase(),w,if(or(w=="orange",w=="apple"),"Fruits",if(or(w=="milk",w=="wine"),"Drinks","")))

This is the same approach as given by Ettore Rizza above but in GREL rather than Jython.

网友答案:

magdmartin and Owen Stephens give good answers. Another simple way using GREL:

  • From the options dropdown for your 'Item' choose Edit column > Add column based on this column...
  • New column name 'Category' and in the expression set:

value.replace("Apple","Fruit").replace("Orange","Fruit").replace("Milk","Drink").replace("Wine","Drink")

You could keep adding .replace("whatever food","whatever category") ad nauseum

相关阅读:
Top