# How to do row and column filtering of a Dataset

**URL:** <https://community.palantir.com/t/how-to-do-row-and-column-filtering-of-a-dataset/2071>\
**Category:** Ask the Community\
**Tags:** code-repositories\
**Created:** [December 2, 2024, 2:48pm UTC](https://community.palantir.com/t/how-to-do-row-and-column-filtering-of-a-dataset/2071 "2024-12-02T14:48:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![raybellwaves](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/raybellwaves/32/902_2.png) [@raybellwaves](https://community.palantir.com/u/raybellwaves)\
**Post date:** [December 2, 2024, 2:48pm UTC](https://community.palantir.com/t/how-to-do-row-and-column-filtering-of-a-dataset/2071/1 "2024-12-02T14:48:27Z")

</div>

It seems the `foundry.transforms.Dataset` API can’t do for row and column filtering at the same time.

I would expect the order doesn’t matter like in SQL or other lazy parquet readers.

I’m working in a Jupyter notebook and doing

```auto
Dataset.get("my_table")\
  .where(Column.get("letter") =="a")\
  .select("column1")\
  .read_table(format="pandas")

```

and get

```auto
ConjureHTTPError: 400 Client Error: Bad Request for url: https://localhost:8080/api/datasets/puzzle_inputs/readTableV2. ErrorCode: 'INVALID_ARGUMENT'. ErrorName: 'Fletcher:MissingFilterColumnException'. ErrorInstanceId: 'XXX'. TraceId: 'XXX'. Parameters: {'missingColumnNames': '[letter]'}

```

```auto
Dataset.get("my_table")\
  .where(Column.get("letter") =="a")\
  .read_table(format="pandas")

```

and

```auto
Dataset.get("my_table")\
  .select("column1")\
  .read_table(format="pandas")

```

work fine

---

<div class="post-metadata">

**Author:** ![arukavina](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/arukavina/32/202_2.png) [@arukavina](https://community.palantir.com/u/arukavina)\
**Post date:** [December 2, 2024, 3:14pm UTC](https://community.palantir.com/t/how-to-do-row-and-column-filtering-of-a-dataset/2071/2 "2024-12-02T15:14:11Z")

</div>

Hi @raybellwaves ,

I suspect this error occurs because the `select` operation removes columns not explicitly included before the `where` filter is applied. This results in the `where` clause failing because the column letter no longer exists in the dataset.

In the foundry.transforms.Dataset API, operations like select and where are applied lazily.

## Why This Happens

- `select` removes unused columns: When you call `select("column1")`, the API only retains the column1 column in the dataset.
- `where` depends on letter: The `where(Column.get("letter") == "a") ` operation requires the letter column to exist in the dataset.
- Order of operations matters: By chaining select before where, you’re effectively trying to filter rows based on a column that has already been removed by the select operation.

## Possible Solution

Retain Columns Required for Filtering

If you must select columns early for some reason, ensure that the column required for filtering (letter in this case) is included in the selection:

```auto
Dataset.get("my_table")\
  .select("column1", "letter")\
  .where(Column.get("letter") == "a")\
  .read_table(format="pandas")

```

Let me know if this worked.

---

<div class="post-metadata">

**Author:** ![raybellwaves](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/raybellwaves/32/902_2.png) [@raybellwaves](https://community.palantir.com/u/raybellwaves)\
**Post date:** [December 2, 2024, 5:36pm UTC](https://community.palantir.com/t/how-to-do-row-and-column-filtering-of-a-dataset/2071/3 "2024-12-02T17:36:16Z")

</div>

Thanks.

It would be nice to have predicate push down in the Dataset API so it can handle any order of operations and do the filtering like in Spark or other tools here: https://sites.google.com/view/raybellwaves/blog/what-data-processing-tool-should-i-use

What i’m actually after is:

```auto
expected = (
    Dataset.get("puzzle_inputs")
    .where(Column.get("file_name") == file_name)
    .read_table(format="pandas")[["input"]]
)

```

---

<div class="post-metadata">

**Author:** ![raybellwaves](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/raybellwaves/32/902_2.png) [@raybellwaves](https://community.palantir.com/u/raybellwaves)\
**Post date:** [December 3, 2024, 8:20am UTC](https://community.palantir.com/t/how-to-do-row-and-column-filtering-of-a-dataset/2071/4 "2024-12-03T08:20:04Z")

</div>

Thanks.

It would be nice to have predicate push down in the API so it can handle any order of operations.

What i’m actually after is

```auto
file_name = "sample_day_01_part_1.txt"
df = Dataset.get("puzzle_inputs").where(Column.get("file_name") == file_name).read_table(format="pandas")[["input"]]

```

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/palantir/user_avatar/community.palantir.com/system/32/3_2.png) [@system](https://community.palantir.com/u/system)\
**Post date:** [February 1, 2025, 8:20am UTC](https://community.palantir.com/t/how-to-do-row-and-column-filtering-of-a-dataset/2071/5 "2025-02-01T08:20:13Z")

</div>

Closed on February 1, 2025, 8:20am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
