The Field steps are responsible for managing a Table Schema's fields. You can add or remove them along with more complex operations like unpacking.
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
steps.field_add(name="note", value="eu", descriptor={"type": "string"}),
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'id', 'type': 'integer'},
{'name': 'name', 'type': 'string'},
{'name': 'population', 'type': 'integer'},
{'name': 'note', 'type': 'string'}]}
+----+-----------+------------+------+
| id | name | population | note |
+====+===========+============+======+
| 1 | 'germany' | 83 | 'eu' |
+----+-----------+------------+------+
| 2 | 'france' | 66 | 'eu' |
+----+-----------+------------+------+
| 3 | 'spain' | 47 | 'eu' |
+----+-----------+------------+------+
Add field. This step can be added using the `steps` parameter for the `transform` function.
(*, title: Optional[str] = None, description: Optional[str] = None, name: str, value: Optional[Any] = None, formula: Optional[Any] = None, function: Optional[Any] = None, position: Optional[int] = None, descriptor: Optional[types.IDescriptor] = None, incremental: bool = False) -> None
A human-oriented name for the field.
str
Specifies value for the field.
Optional[Any]
Evaluatable expressions to set the value for the field. The expressions are processed using simpleeval library.
Optional[Any]
Python function to set the value for the field.
Optional[Any]
Position index where to add the field. For example, to add the field in second position, we need to set it as 'position=2'.
Optional[int]
A dictionary, which contains metadata for the field which describes the properties of the field.
Optional[types.IDescriptor]
Indicates if it is an incremental value. If True, the sequential value is set to the new field. The default value is false.
bool
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
steps.field_filter(names=["id", "name"]),
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'id', 'type': 'integer'},
{'name': 'name', 'type': 'string'}]}
+----+-----------+
| id | name |
+====+===========+
| 1 | 'germany' |
+----+-----------+
| 2 | 'france' |
+----+-----------+
| 3 | 'spain' |
+----+-----------+
Filter fields. This step can be added using the `steps` parameter for the `transform` function.
(*, name: Optional[str] = None, title: Optional[str] = None, description: Optional[str] = None, names: List[str]) -> None
Names of the field to be read. Other fields will be ignored.
List[str]
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
# seperator argument can be used to set delimeter. Default value is '-'
# preserve argument keeps the original fields
steps.field_merge(name="details", from_names=["name", "population"], preserve=True)
],
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'id', 'type': 'integer'},
{'name': 'name', 'type': 'string'},
{'name': 'population', 'type': 'integer'},
{'name': 'details', 'type': 'string'}]}
+----+-----------+------------+--------------+
| id | name | population | details |
+====+===========+============+==============+
| 1 | 'germany' | 83 | 'germany-83' |
+----+-----------+------------+--------------+
| 2 | 'france' | 66 | 'france-66' |
+----+-----------+------------+--------------+
| 3 | 'spain' | 47 | 'spain-47' |
+----+-----------+------------+--------------+
Merge fields. This step can be added using the `steps` parameter for the `transform` function.
(*, title: Optional[str] = None, description: Optional[str] = None, name: str, from_names: List[str], separator: str = -, preserve: bool = False) -> None
Name of the new field that will be created after merge.
str
List of field names to merge.
List[str]
Separator to use while merging values of the two fields.
str
It indicates if the fields are preserved or not after merging. If True, fields will not be removed and vice versa.
bool
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
steps.field_move(name="id", position=3),
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'name', 'type': 'string'},
{'name': 'population', 'type': 'integer'},
{'name': 'id', 'type': 'integer'}]}
+-----------+------------+----+
| name | population | id |
+===========+============+====+
| 'germany' | 83 | 1 |
+-----------+------------+----+
| 'france' | 66 | 2 |
+-----------+------------+----+
| 'spain' | 47 | 3 |
+-----------+------------+----+
Move field. This step can be added using the `steps` parameter for the `transform` function.
(*, title: Optional[str] = None, description: Optional[str] = None, name: str, position: int) -> None
Field name to move.
str
New position for the field being moved.
int
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
# field_type returns packed fields as JSON Object. Default value for field_type is 'array'
# preserve argument keeps the original fields
steps.field_pack(name="details", from_names=["name", "population"], as_object=True, preserve=True)
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'id', 'type': 'integer'},
{'name': 'name', 'type': 'string'},
{'name': 'population', 'type': 'integer'},
{'name': 'details', 'type': 'object'}]}
+----+-----------+------------+-----------------------------------------+
| id | name | population | details |
+====+===========+============+=========================================+
| 1 | 'germany' | 83 | {'name': 'germany', 'population': '83'} |
+----+-----------+------------+-----------------------------------------+
| 2 | 'france' | 66 | {'name': 'france', 'population': '66'} |
+----+-----------+------------+-----------------------------------------+
| 3 | 'spain' | 47 | {'name': 'spain', 'population': '47'} |
+----+-----------+------------+-----------------------------------------+
Pack fields. This step can be added using the `steps` parameter for the `transform` function.
(*, title: Optional[str] = None, description: Optional[str] = None, name: str, from_names: List[str], as_object: bool = False, preserve: bool = False) -> None
Name of the new field.
str
List of fields to be packed.
List[str]
The packed value of the field will be stored as object if set to True.
bool
Specifies if the field should be preserved or not. If True, fields part of packing process will be preserved.
bool
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
steps.field_remove(names=["id"]),
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'name', 'type': 'string'},
{'name': 'population', 'type': 'integer'}]}
+-----------+------------+
| name | population |
+===========+============+
| 'germany' | 83 |
+-----------+------------+
| 'france' | 66 |
+-----------+------------+
| 'spain' | 47 |
+-----------+------------+
Remove field. This step can be added using the `steps` parameter for the `transform` function.
(*, name: Optional[str] = None, title: Optional[str] = None, description: Optional[str] = None, names: List[str]) -> None
List of fields to remove.
List[str]
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
steps.field_split(name="name", to_names=["name1", "name2"], pattern="a"),
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'id', 'type': 'integer'},
{'name': 'population', 'type': 'integer'},
{'name': 'name1', 'type': 'string'},
{'name': 'name2', 'type': 'string'}]}
+----+------------+--------+-------+
| id | population | name1 | name2 |
+====+============+========+=======+
| 1 | 83 | 'germ' | 'ny' |
+----+------------+--------+-------+
| 2 | 66 | 'fr' | 'nce' |
+----+------------+--------+-------+
| 3 | 47 | 'sp' | 'in' |
+----+------------+--------+-------+
Split field. This step can be added using the `steps` parameter for the `transform` function.
(*, title: Optional[str] = None, description: Optional[str] = None, name: str, to_names: List[str], pattern: str, preserve: bool = False) -> None
Name of the field to split.
str
List of names of new fields.
List[str]
Pattern to split the field value, for example: "a".
str
Whether to preserve the fields after the split. If True, the fields are not removed after split.
bool
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
steps.field_update(name="id", value=[1, 1], descriptor={"type": "string"}),
steps.field_unpack(name="id", to_names=["id2", "id3"]),
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'name', 'type': 'string'},
{'name': 'population', 'type': 'integer'},
{'name': 'id2', 'type': 'any'},
{'name': 'id3', 'type': 'any'}]}
+-----------+------------+-----+-----+
| name | population | id2 | id3 |
+===========+============+=====+=====+
| 'germany' | 83 | 1 | 1 |
+-----------+------------+-----+-----+
| 'france' | 66 | 1 | 1 |
+-----------+------------+-----+-----+
| 'spain' | 47 | 1 | 1 |
+-----------+------------+-----+-----+
Unpack field. This step can be added using the `steps` parameter for the `transform` function.
(*, title: Optional[str] = None, description: Optional[str] = None, name: str, to_names: List[str], preserve: bool = False) -> None
Name of the field to unpack.
str
List of names for new fields that will be created after unpacking.
List[str]
Whether to preserve the source fields after unpacking.
bool
from pprint import pprint
from frictionless import Package, Resource, transform, steps
source = Resource(path="transform.csv")
target = transform(
source,
steps=[
steps.field_update(name="id", value=str, descriptor={"type": "string"}),
]
)
print(target.schema)
print(target.to_view())
{'fields': [{'name': 'id', 'type': 'string'},
{'name': 'name', 'type': 'string'},
{'name': 'population', 'type': 'integer'}]}
+------+-----------+------------+
| id | name | population |
+======+===========+============+
| None | 'germany' | 83 |
+------+-----------+------------+
| None | 'france' | 66 |
+------+-----------+------------+
| None | 'spain' | 47 |
+------+-----------+------------+
Update field. This step can be added using the `steps` parameter for the `transform` function.
(*, title: Optional[str] = None, description: Optional[str] = None, name: str, value: Optional[Any] = None, formula: Optional[Any] = None, function: Optional[Any] = None, descriptor: Optional[types.IDescriptor] = None) -> None
Name of the field to update.
str
Cell value to set for the field.
Optional[Any]
Evaluatable expressions to set the value for the field. The expressions are processed using simpleeval library.
Optional[Any]
Python function to set the value for the field.
Optional[Any]
A descriptor for the field to set the metadata.
Optional[types.IDescriptor]