-
Notifications
You must be signed in to change notification settings - Fork 195
Transforming Data
#Transforming Data Karma offers to commands to transform the values in cells to create new columns.
-
PyTransform
allows you to type expressions in Python to define data transformations -
Transform
allows you to define the tranformations by providing examples.
You invoke both commands from the menu of commands available on all columns:
##Transformations Using Python
The screenshot below illustrates the use of the PyTransform
command.
Suppose you want to transform the Measurements
column to extract the measurements of artwork frames. These are the parts of the string following the letter f
(e.g., f 14 1/2 x 32 1/4
).
You select PyTransform
in the menu of the Measurements
column, and Karma shows you the following dialogue where you specify how to perform the transformation:
The main part of the dialogue is the area where you enter your Python code.
Here you enter code that specifies how to compute the value of a cell in the new column as a function of the values of the cells in the same row.
Using the getValue
function you can access the value of any cell in a row.
For example getValue("Measurements")
gets the value of the cell in the Measurements
column.
The sample code in the screenshot above shows how to find the index of the string f
and then extract all characters from that index to the end of the string.
You can test your code by clicking on the Preview Results for Top 5 Rows
button.
If your code has errors, you can click on the View Errors
button to see the errors that the Python interpreter generates for the top 5 rows.
You can access the values of cells in all columns by calling getValue
using the name of the column.
For example, you can easily combine the values of two columns, e.g., first
and last
to create a new column name
using the code return getValue("first")+" "+getValue("last")
.
Hierarchical Sources (e.g., XML or JSON): if your source has nested columns, there is a restriction of which columns you can access.
For example, if you invoke PyTransform
on the relatedArtworksTitle
column, you can access the value of the id
column using getValue("id")
because it is in the same nested table as relatedArtworksTitle
.
You can access the value of the nationality
column using getValue("nationality")
because it is in an enclosing table.
Now, if you invoke PyTransform
on the nationality
, you cannot access the value of the id
or relatedArtworksTitle
columns because they are in tables one level of nesting deeper than the nationality
column. You can, of course, access values in columns at the same level of nationality
or above.
If you are not familiar with Python, you can learn the basics in a few minutes at http://www.learnpython.org.
##Transformations Using Examples
Karma also provides an experimental feature where you can define transformations by providing examples of how you want to transform the data (use the Transform
command in the column menu).
This capability is useful for small data sets where you can verify that your data was transformed correctly. The feature is experimental and we do not recommend using it in production settings where you are using Karma to generate RDF for large datasets.