Filip Blaauw
Filip Blaauw

Reputation: 761

Assign rows to category in Openrefine

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?

Upvotes: 0

Views: 158

Answers (3)

jmk
jmk

Reputation: 486

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

Upvotes: 0

Owen Stephens
Owen Stephens

Reputation: 1560

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.

Upvotes: 1

magdmartin
magdmartin

Reputation: 1787

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.

Upvotes: 1

Related Questions