Rows.__getitem__()#
relationalai
#Rows.__getitem__(index: int|str) -> Expression
Gets an Expression
object representing a column in the Rows
object using subscript notation.
If Rows
was created from a list of tuples, then index
must be an integer index.
If Rows
was created from a list of dictionaries, then index
must be a string key.
Parameters#
Name | Type | Description |
---|---|---|
index | int or str | The integer index or string key of the column to get from the Rows object. |
Returns#
An Expression
object.
Example#
Rows.__getitem__()
allows you to access columns in a Rows
object using subscript notation:
#import relationalai as rai
from relationalai.std import as_rows
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Company = model.Type("Company")
Person = model.Type("Person")
# =======
# EXAMPLE
# =======
# Define Company objects from a list of tuples.
with model.rule():
row = as_rows([(1, "RelationalAI"), (2, "Snowflake")])
Company.add(id=row[0]).set(name=row[1])
# Define Person objects from a list of dictionaries.
with model.rule():
row = as_rows([
{"id": 1, "name": "Alice", "age": 30},
{"id": 2, "name": "Bob", "age": 45},
])
Person.add(id=row["id"]).set(name=row["name"], age=row["age"])