## Pandas DataFrame Parser

A Pandas DataFrame is a popular data structure in the Python programming language, commonly used for data manipulation and analysis. It provides a comprehensive set of tools for working with structured data, making it a versatile option for tasks such as data cleaning, transformation, and analysis.

This output parser allows users to specify an arbitrary Pandas DataFrame and query LLMs for data in the form of a formatted dictionary that extracts data from the corresponding DataFrame. Keep in mind that large language models are leaky abstractions! You’ll have to use an LLM with sufficient capacity to generate a well-formed query as per the defined format instructions.

Use Pandas’ DataFrame object to declare the DataFrame you wish to perform queries on.

```
import pprintfrom typing import Any, Dictimport pandas as pdfrom langchain.output_parsers import PandasDataFrameOutputParserfrom langchain_core.prompts import PromptTemplatefrom langchain_openai import ChatOpenAI
```

```
model = ChatOpenAI(temperature=0)
```

```
# Solely for documentation purposes.def format_parser_output(parser_output: Dict[str, Any]) -> None:    for key in parser_output.keys():        parser_output[key] = parser_output[key].to_dict()    return pprint.PrettyPrinter(width=4, compact=True).pprint(parser_output)
```

```
# Define your desired Pandas DataFrame.df = pd.DataFrame(    {        "num_legs": [2, 4, 8, 0],        "num_wings": [2, 0, 0, 0],        "num_specimen_seen": [10, 2, 1, 8],    })# Set up a parser + inject instructions into the prompt template.parser = PandasDataFrameOutputParser(dataframe=df)
```

```
# Here's an example of a column operation being performed.df_query = "Retrieve the num_wings column."# Set up the prompt.prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)chain = prompt | model | parserparser_output = chain.invoke({"query": df_query})format_parser_output(parser_output)
```

```
{'num_wings': {0: 2,               1: 0,               2: 0,               3: 0}}
```

```
# Here's an example of a row operation being performed.df_query = "Retrieve the first row."# Set up the prompt.prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)chain = prompt | model | parserparser_output = chain.invoke({"query": df_query})format_parser_output(parser_output)
```

```
{'0': {'num_legs': 2,       'num_specimen_seen': 10,       'num_wings': 2}}
```

```
# Here's an example of a random Pandas DataFrame operation limiting the number of rowsdf_query = "Retrieve the average of the num_legs column from rows 1 to 3."# Set up the prompt.prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)chain = prompt | model | parserparser_output = chain.invoke({"query": df_query})print(parser_output)
```

```
# Here's an example of a poorly formatted querydf_query = "Retrieve the mean of the num_fingers column."# Set up the prompt.prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)chain = prompt | model | parserparser_output = chain.invoke({"query": df_query})
```

```
OutputParserException: Invalid column: num_fingers. Please check the format instructions.
```

Find out api documentation for [PandasDataFrameOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.pandas_dataframe.PandasDataFrameOutputParser.html#langchain.output_parsers.pandas_dataframe.PandasDataFrameOutputParser).