| 1 | # Pandas and PyArrow |
| 2 | |
| 3 | Because Lance is built on top of [Apache Arrow](https://arrow.apache.org/), |
| 4 | LanceDB is tightly integrated with the Python data ecosystem, including [Pandas](https://pandas.pydata.org/) |
| 5 | and PyArrow. The sequence of steps in a typical workflow is shown below. |
| 6 | |
| 7 | ## Create dataset |
| 8 | |
| 9 | First, we need to connect to a LanceDB database. |
| 10 | |
| 11 | ```py |
| 12 | |
| 13 | import lancedb |
| 14 | |
| 15 | db = lancedb.connect("data/sample-lancedb") |
| 16 | ``` |
| 17 | |
| 18 | We can load a Pandas `DataFrame` to LanceDB directly. |
| 19 | |
| 20 | ```py |
| 21 | import pandas as pd |
| 22 | |
| 23 | data = pd.DataFrame({ |
| 24 | "vector": [[3.1, 4.1], [5.9, 26.5]], |
| 25 | "item": ["foo", "bar"], |
| 26 | "price": [10.0, 20.0] |
| 27 | }) |
| 28 | table = db.create_table("pd_table", data=data) |
| 29 | ``` |
| 30 | |
| 31 | Similar to the [`pyarrow.write_dataset()`](https://arrow.apache.org/docs/python/generated/pyarrow.dataset.write_dataset.html) method, LanceDB's |
| 32 | [`db.create_table()`](python.md/#lancedb.db.DBConnection.create_table) accepts data in a variety of forms. |
| 33 | |
| 34 | If you have a dataset that is larger than memory, you can create a table with `Iterator[pyarrow.RecordBatch]` to lazily load the data: |
| 35 | |
| 36 | ```py |
| 37 | |
| 38 | from typing import Iterable |
| 39 | import pyarrow as pa |
| 40 | |
| 41 | def make_batches() -> Iterable[pa.RecordBatch]: |
| 42 | for i in range(5): |
| 43 | yield pa.RecordBatch.from_arrays( |
| 44 | [ |
| 45 | pa.array([[3.1, 4.1], [5.9, 26.5]]), |
| 46 | pa.array(["foo", "bar"]), |
| 47 | pa.array([10.0, 20.0]), |
| 48 | ], |
| 49 | ["vector", "item", "price"]) |
| 50 | |
| 51 | schema=pa.schema([ |
| 52 | pa.field("vector", pa.list_(pa.float32())), |
| 53 | pa.field("item", pa.utf8()), |
| 54 | pa.field("price", pa.float32()), |
| 55 | ]) |
| 56 | |
| 57 | table = db.create_table("iterable_table", data=make_batches(), schema=schema) |
| 58 | ``` |
| 59 | |
| 60 | You will find detailed instructions of creating a LanceDB dataset in |
| 61 | [Getting Started](../basic.md#quick-start) and [API](python.md/#lancedb.db.DBConnection.create_table) |
| 62 | sections. |
| 63 | |
| 64 | ## Vector search |
| 65 | |
| 66 | We can now perform similarity search via the LanceDB Python API. |
| 67 | |
| 68 | ```py |
| 69 | # Open the table previously created. |
| 70 | table = db.open_table("pd_table") |
| 71 | |
| 72 | query_vector = [100, 100] |
| 73 | # Pandas DataFrame |
| 74 | df = table.search(query_vector).limit(1).to_pandas() |
| 75 | print(df) |
| 76 | ``` |
| 77 | |
| 78 | ``` |
| 79 | vector item price _distance |
| 80 | 0 [5.9, 26.5] bar 20.0 14257.05957 |
| 81 | ``` |
| 82 | |
| 83 | If you have a simple filter, it's faster to provide a `where` clause to LanceDB's `search` method. |
| 84 | For more complex filters or aggregations, you can always resort to using the underlying `DataFrame` methods after performing a search. |
| 85 | |
| 86 | ```python |
| 87 | |
| 88 | # Apply the filter via LanceDB |
| 89 | results = table.search([100, 100]).where("price < 15").to_pandas() |
| 90 | assert len(results) == 1 |
| 91 | assert results["item"].iloc[0] == "foo" |
| 92 | |
| 93 | # Apply the filter via Pandas |
| 94 | df = results = table.search([100, 100]).to_pandas() |
| 95 | results = df[df.price < 15] |
| 96 | assert len(results) == 1 |
| 97 | assert results["item"].iloc[0] == "foo" |
| 98 | ``` |