OpenRefine: create a shifted copy of a column

杀马特。学长 韩版系。学妹 提交于 2019-12-11 04:38:49

问题


I wonder if OpenRefine lets you access data from other rows, when creating a new column. I suspect it does not (and it would be a sane design principle) but there could be a hack around that.

Here is an example of what one could want to do: shifting a column by one row.

I have the following table:

╔═════╦════════╗
║ row ║ Model  ║
╠═════╬════════╣
║   1 ║ Quest  ║
║   2 ║ DF     ║
║   3 ║ Waw    ║
║   4 ║ Strada ║
╚═════╩════════╝

And I want to obtain the following result:

╔═════╦════════╦══════════╗
║ row ║ Model  ║ Previous ║
╠═════╬════════╬══════════╣
║   1 ║ Quest  ║          ║
║   2 ║ DF     ║ Quest    ║
║   3 ║ Waw    ║ DF       ║
║   4 ║ Strada ║ Waw      ║
╚═════╩════════╩══════════╝

Looking at https://github.com/OpenRefine/OpenRefine/wiki/Variables it seems that there isn't any variable that would let you access information outside the current row or record, so I wonder if this sort of operation is possible.


回答1:


Unfortunately, there is no "column" variable in Open Refine. A possible workaround would be to turn all the dataset into a single record, then apply a bit of Python/Jython.

Example:

data = row['record']['cells']['Model']['value']
for i, el in enumerate(data):
    if value == el and i !=0:
        return data[i - 1]

Screencast:

I don't know if a solution in GREL is possible.



来源:https://stackoverflow.com/questions/46379241/openrefine-create-a-shifted-copy-of-a-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!