I'm not sure there's a straightforward way to filter the way you're wanting to. One option you do have is to create a separate DataFrame containing the names you want to filter on and perform a join:
let filter_df = df! [
"name" => ["panda", "seahorse"]
].unwrap();
let avg = df.lazy()
.join(filter_df.lazy(), [col("name")], [col("name")], JoinType::Inner)
.select([col("age").mean()]);
It was unclear whether you wanted to perform the mean on all the ages returned from this filtering (this is how it was worded in your question), if that's the case then this code should work for you. This is the output:
Ok(shape: (1, 1)
┌─────┐
│ age │
│ --- │
│ f64 │
╞═════╡
│ 3.0 │
└─────┘)
Your code makes it seem like you were trying to calculate the mean for all pandas vs the mean for all seahorses. If that's the case, then you can use a .groupby().agg() chain:
let avg = df.lazy()
.join(names_df.lazy(), [col("name")], [col("name")], JoinType::Inner)
.groupby([col("name")])
.agg([col("age").mean()]);
Which produces:
Ok(shape: (2, 2)
┌──────────┬─────┐
│ name ┆ age │
│ --- ┆ --- │
│ str ┆ f64 │
╞══════════╪═════╡
│ seahorse ┆ 1.0 │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
│ panda ┆ 5.0 │
└──────────┴─────┘)