> For the complete documentation index, see [llms.txt](https://fennaf.gitbook.io/bfvm19prog1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fennaf.gitbook.io/bfvm19prog1/data-wrangling/reshape-with-melt.md).

# Reshape with melt

The pandas.DataFrame.melt() function is useful to massage a DataFrame into a format where one or more columns are identifier variables (id\_vars), while all other columns, considered measured variables (value\_vars). The function “unpivotes” to the row axis, leaving just two non-identifier columns, ‘variable’ and ‘value’.&#x20;

To demonstrate this we will work with the EEG brain dataset. The values are in the X\<number> columns, the variable of interest is the 'y'&#x20;

```python
import numpy as np
import pandas as pd

from bokeh.plotting import figure, output_file, show
df = pd.read_csv('data/eeg_data.csv').rename(columns={"Unnamed: 0": "ID"})
df = df.drop(columns='ID')
df.head()
```

|   | X1   | X2   | X3  | X4   | X5  | X6  | X7   | X8   | X9  | X10 | ... | X170 | X171 | X172 | X173 | X174 | X175 | X176 | X177 | X178 | y |
| - | ---- | ---- | --- | ---- | --- | --- | ---- | ---- | --- | --- | --- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | - |
| 0 | 135  | 190  | 229 | 223  | 192 | 125 | 55   | -9   | -33 | -38 | ... | -17  | -15  | -31  | -77  | -103 | -127 | -116 | -83  | -51  | 4 |
| 1 | 386  | 382  | 356 | 331  | 320 | 315 | 307  | 272  | 244 | 232 | ... | 164  | 150  | 146  | 152  | 157  | 156  | 154  | 143  | 129  | 1 |
| 2 | -32  | -39  | -47 | -37  | -32 | -36 | -57  | -73  | -85 | -94 | ... | 57   | 64   | 48   | 19   | -12  | -30  | -35  | -35  | -36  | 5 |
| 3 | -105 | -101 | -96 | -92  | -89 | -95 | -102 | -100 | -87 | -79 | ... | -82  | -81  | -80  | -77  | -85  | -77  | -72  | -69  | -65  | 5 |
| 4 | -9   | -65  | -98 | -102 | -78 | -48 | -16  | 0    | -21 | -59 | ... | 4    | 2    | -12  | -32  | -41  | -65  | -83  | -89  | -73  | 5 |

5 rows × 179 columns

We can melt this by the function melt. It will keep the 'y' value and put all the other columns in the variable column

```python
dfm = df.melt(id_vars=['y'])
dfm.head()
 
```

|   | y | variable | value |
| - | - | -------- | ----- |
| 0 | 4 | X1       | 135   |
| 1 | 1 | X1       | 386   |
| 2 | 5 | X1       | -32   |
| 3 | 5 | X1       | -105  |
| 4 | 5 | X1       | -9    |

This might be handy if I want for instance to groupby y-value to discover the differences in counts, mean or standard deviation. I also can make a graphical overview&#x20;

```python
dfm.y.value_counts()
dfm.groupby('y').std()
dfm.groupby('y').mean()
```

```python
import seaborn as sns
sns.violinplot(x=dfm.y, y=dfm.value)
```

![violinplot of dfm](/files/-MKjLqT51omv7_3EUzLh)


---

# 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://fennaf.gitbook.io/bfvm19prog1/data-wrangling/reshape-with-melt.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.
