# DataFrame Operations

DataFrame operations provide powerful data manipulation capabilities for structured data processing within BroxiAI workflows.

## DataFrame Operations

This component performs operations on [DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) rows and columns.

To use this component in a flow, connect a component that outputs [DataFrame](https://guidenai.gitbook.io/broxi/data-processing/dataframe-operations) to the **DataFrame Operations** component.

This example fetches JSON data from an API. The **Lambda filter** component extracts and flattens the results into a tabular DataFrame. The **DataFrame Operations** component can then work with the retrieved data.

1. The **API Request** component retrieves data with only `source` and `result` fields. For this example, the desired data is nested within the `result` field.
2. Connect a **Lambda Filter** to the API request component, and a **Language model** to the **Lambda Filter**. This example connects a **Groq** model component.
3. In the **Groq** model component, add your **Groq** API key.
4. To filter the data, in the **Lambda filter** component, in the **Instructions** field, use natural language to describe how the data should be filtered. For this example, enter:

`I want to explode the result column out into a Data object`

{% hint style="warning" %}
Avoid punctuation in the **Instructions** field, as it can cause errors.
{% endhint %}

5. To run the flow, in the **Lambda Filter** component, click .
6. To inspect the filtered data, in the **Lambda Filter** component, click . The result is a structured DataFrame.

```markdown
id | name             | company               | username        | email                              | address           | zip
---|------------------|----------------------|-----------------|------------------------------------|-------------------|-------
1  | Emily Johnson    | ABC Corporation      | emily_johnson   | emily.johnson@abccorporation.com   | 123 Main St       | 12345
2  | Michael Williams | XYZ Corp             | michael_williams| michael.williams@xyzcorp.com       | 456 Elm Ave       | 67890
```

7. Add the **DataFrame Operations** component, and a **Chat Output** component to the flow.
8. In the **DataFrame Operations** component, in the **Operation** field, select **Filter**.
9. To apply a filter, in the **Column Name** field, enter a column to filter on. This example filters by `name`.
10. Click **Playground**, and then click **Run Flow**. The flow extracts the values from the `name` column.

> name\
> Emily Johnson\
> Michael Williams\
> John Smith\
> ...

## Operations

This component can perform the following operations on Pandas [DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html).

| Operation      | Description                             | Required Inputs                                  |
| -------------- | --------------------------------------- | ------------------------------------------------ |
| Add Column     | Adds a new column with a constant value | new\_column\_name, new\_column\_value            |
| Drop Column    | Removes a specified column              | column\_name                                     |
| Filter         | Filters rows based on column value      | column\_name, filter\_value                      |
| Head           | Returns first n rows                    | num\_rows                                        |
| Rename Column  | Renames an existing column              | column\_name, new\_column\_name                  |
| Replace Value  | Replaces values in a column             | column\_name, replace\_value, replacement\_value |
| Select Columns | Selects specific columns                | columns\_to\_select                              |
| Sort           | Sorts DataFrame by column               | column\_name, ascending                          |
| Tail           | Returns last n rows                     | num\_rows                                        |

### Inputs

| Name                | Display Name      | Info                                                                                                                                                |
| ------------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| df                  | DataFrame         | The input DataFrame to operate on.                                                                                                                  |
| operation           | Operation         | Select the DataFrame operation to perform. Options: Add Column, Drop Column, Filter, Head, Rename Column, Replace Value, Select Columns, Sort, Tail |
| column\_name        | Column Name       | The column name to use for the operation.                                                                                                           |
| filter\_value       | Filter Value      | The value to filter rows by.                                                                                                                        |
| ascending           | Sort Ascending    | Whether to sort in ascending order.                                                                                                                 |
| new\_column\_name   | New Column Name   | The new column name when renaming or adding a column.                                                                                               |
| new\_column\_value  | New Column Value  | The value to populate the new column with.                                                                                                          |
| columns\_to\_select | Columns to Select | List of column names to select.                                                                                                                     |
| num\_rows           | Number of Rows    | Number of rows to return (for head/tail). Default: 5                                                                                                |
| replace\_value      | Value to Replace  | The value to replace in the column.                                                                                                                 |
| replacement\_value  | Replacement Value | The value to replace with.                                                                                                                          |

### Outputs

| Name   | Display Name | Info                                         |
| ------ | ------------ | -------------------------------------------- |
| output | DataFrame    | The resulting DataFrame after the operation. |

## Usage Notes

* **Comprehensive Operations**: Full range of common DataFrame manipulations
* **Flexible Filtering**: Multiple ways to filter and select data
* **Column Management**: Add, remove, rename, and modify columns
* **Data Analysis**: Sort, head, and tail operations for data exploration
* **Value Replacement**: Find and replace functionality for data cleaning
* **Integration Ready**: Works seamlessly with other BroxiAI components
