# Loading Querying And Filtering Data Using The Csv Module

## Reading CSV files

We can read a CSV file using the built-in `csv` module.

```py
import csv

rows = []

f = open('file.csv')
reader = csv.DictReader(f)
print(reader.fieldnames) # shows all available fields

for row in reader:
  rows.append(row)
 
f.close()

# Now you can access your row data in `rows`
```

**Note**: You can only iterate through a `reader` once.

## List Comprehensions

**List comprehensions** are expressions that allow you to create new lists out of existing lists using a mix of filter conditions and mapping conditions.

```py
people = [{ "name": "Dan", "age": 29 }, { "name": "John", "age": 30 }]
ages_below_30 = [person["age"] for person in people if person["age"] < 30]
```

The basic syntax is something like...

```py
[<mapping> for item in list if <filter>]
```

## Lambda functions

A **lambda** is a function expression: basically, a way to create functions inline.

```py
fn1 = lambda x: x + 2
```

Lambdas are especially useful if some function accepts a function as an argument. Common examples include `sorted`, `min`, and `max`

```py
people = [{ "name": "Dan", "age": 29 }, { "name": "John", "age": 30 }]
oldest_person = max(people, key = lambda x: x["age"])
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.danfitz.com/python/data-analysis-using-python/a-loading-querying-filtering-data-csv-module.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
