> For the complete documentation index, see [llms.txt](https://danfo.jsdata.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://danfo.jsdata.org/api-reference/dataframe/danfo.dataframe.groupby.md).

# DataFrame.groupby

danfo.DataFrame.**groupby**(columns)

| Parameters | Type  | Description                                           | Default |
| ---------- | ----- | ----------------------------------------------------- | ------- |
| columns    | Array | The names of a column(s) in the DataFrame to group by |         |

## **Examples**

## **Groupby a single column from a DataFrame**

{% tabs %}
{% tab title="Node" %}

```javascript
const dfd = require("danfojs-node")

let data = [["Pear", 2, 3], ["Pear", 5, 6], ["Apple", 30, 40], ["Apple", 89, 78]]
let cols = ["A", "B", "C"]

let df = new dfd.DataFrame(data, { columns: cols })
let group_df = df.groupby(["A"])
console.log(group_df)
```

```
// ouput
Groupby {
  colDict: {
    Pear: { A: [Array], B: [Array], C: [Array] },
    Apple: { A: [Array], B: [Array], C: [Array] }
  },
  keyToValue: { Pear: [ 'Pear' ], Apple: [ 'Apple' ] },
  keyCol: [ 'A' ],
  data: [
    [ 'Pear', 2, 3 ],
    [ 'Pear', 5, 6 ],
    [ 'Apple', 30, 40 ],
    [ 'Apple', 89, 78 ]
  ],
  columnName: [ 'A', 'B', 'C' ],
  colDtype: [ 'string' ],
  colIndex: [ 0 ]
}
```

{% endtab %}

{% tab title="Browser" %}

```
```

{% endtab %}
{% endtabs %}

A groupby operation will return a GroupBy class object. You can apply any of the following operation on the groupby result:

1. [count](/api-reference/dataframe/danfo.dataframe.count.md)
2. [sum](/api-reference/dataframe/danfo.dataframe.sum.md)
3. [std](/api-reference/dataframe/danfo.dataframe.std.md)
4. [var](/api-reference/dataframe/danfo.dataframe.var.md)
5. [mean](/api-reference/dataframe/danfo.dataframe.mean.md)
6. [cumSum](/api-reference/dataframe/danfo.dataframe.cumsum.md)
7. [cumMax](/api-reference/dataframe/danfo.dataframe.cummax.md)
8. [cumProd](/api-reference/dataframe/danfo.dataframe.cumprod.md)
9. [cumMin](/api-reference/dataframe/danfo.dataframe.cummin.md)
10. [max](/api-reference/dataframe/danfo.dataframe.max.md)
11. [min](/api-reference/dataframe/danfo.dataframe.min.md)

## Example of Groupby and apply a sum function

{% tabs %}
{% tab title="Node" %}

```javascript
const dfd = require("danfojs-node")

let data = [["Pear", 2, 3], ["Pear", 5, 6], ["Apple", 30, 40], ["Apple", 89, 78]]
let cols = ["A", "B", "C"]
let df = new dfd.DataFrame(data, { columns: cols })
let group_df = df.groupby(["A"]).sum()

group_df.print()
```

{% endtab %}
{% endtabs %}

```
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B_sum             │ C_sum             ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0          │ Pear              │ 7                 │ 9                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1          │ Apple             │ 119               │ 118               ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
```

## **Groupby a two columns from a DataFrame**

{% tabs %}
{% tab title="Node" %}

```javascript
const dfd = require("danfojs-node")

let data = [["Pear", 2, 3], ["Pear", 2, 6], ["Apple", 30, 40], ["Apple", 89, 78]]
let cols = ["A", "B", "C"]
let df = new dfd.DataFrame(data, { columns: cols })
let group_df = df.groupby(["A", "B"]).sum()

group_df.print()
```

{% endtab %}

{% tab title="Browser" %}

```
```

{% endtab %}
{% endtabs %}

```
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C_sum             ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0          │ Pear              │ 2                 │ 9                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1          │ Apple             │ 30                │ 40                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 2          │ Apple             │ 89                │ 78                ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://danfo.jsdata.org/api-reference/dataframe/danfo.dataframe.groupby.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.
