> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Convert Pivot Table  to Regular Data Frame in Pandas
- URL: https://datascientyst.com/convert-pivot-table-to-regular-data-frame-in-pandas/
- Published: 2023-04-01T06:43:52.000Z
- Updated: 2023-04-01T06:43:52.000Z
- Author: John D K
- Tags: Pivot

In this post, we will see how to convert a Pandas pivot table to a regular DataFrame.

To convert pivot table to DataFrame we can use:

**(1) the `reset_index()` method**

```python
df_p.set_axis(df_p.columns.tolist(), axis=1).reset_index()

```

**(2) to\_records() + pd.DataFrame()**

```python
pd.DataFrame(df_p.to_records())

```

Let's cover both ways in detail in the next sections.

![](https://datascientyst.com/content/images/2023/04/convert-pivot-table-to-regular-data-frame-in-pandas.webp)

## Setup

First, let's create the DataFrame with 4 columns:

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

data = {'date': np.random.choice([202303, 202304], size=100),
    	'code': np.random.choice([*'ABC'], size=100),
    	'type': np.random.choice([*'RB'], size=100),
    	'val': np.arange(100)}
df = pd.DataFrame(data)
df

```

First 5 rows of this DataFrame are:

|   | date   | code | type | val |
| - | ------ | ---- | ---- | --- |
| 0 | 202304 | B    | B    | 0   |
| 1 | 202304 | A    | B    | 1   |
| 2 | 202304 | B    | B    | 2   |
| 3 | 202303 | C    | B    | 3   |
| 4 | 202304 | A    | B    | 4   |

We can create pivot table from above data by:

```python
df_p = df.pivot_table(index=['date','code'], columns='type', values='val', aggfunc="count")
df_p

```

result:

|        | type | B  | R |
| ------ | ---- | -- | - |
| date   | code |    |   |
| 202303 | A    | 9  | 5 |
| B      | 9    | 10 |   |
| C      | 11   | 12 |   |
| 202304 | A    | 11 | 6 |
| B      | 10   | 4  |   |
| C      | 6    | 7  |   |

Let's see how to convert the pivot table back to normal DataFrame. Essentially this means to remove the MultiIndex or flatten the DataFrame.

## reset\_index()

To convert pivot table to a normal DataFrame in Pandas, we can combine:

- `reset_index()` method
- `set_axis()`

We can flatten the pivot table by removing the MultiIndex:

```python
df_p.set_axis(df_p.columns.tolist(), axis=1).reset_index()

```

The result is:

|   | date   | code | B  | R  |
| - | ------ | ---- | -- | -- |
| 0 | 202303 | A    | 9  | 5  |
| 1 | 202303 | B    | 9  | 10 |
| 2 | 202303 | C    | 11 | 12 |
| 3 | 202304 | A    | 11 | 6  |
| 4 | 202304 | B    | 10 | 4  |
| 5 | 202304 | C    | 6  | 7  |

## to\_records()

Another way to convert pivot tables in Pandas is by:

- extracting data with `to_records()`
- create new DataFrame

```python
pd.DataFrame(df_p.to_records())

```

We get the same result:

|   | date   | code | B  | R  |
| - | ------ | ---- | -- | -- |
| 0 | 202303 | A    | 9  | 5  |
| 1 | 202303 | B    | 9  | 10 |
| 2 | 202303 | C    | 11 | 12 |
| 3 | 202304 | A    | 11 | 6  |
| 4 | 202304 | B    | 10 | 4  |
| 5 | 202304 | C    | 6  | 7  |

## Summary

We saw how to convert pivot tables to normal DataFrame in Pandas. This is useful when we need to work with pivot tables as regular DataFrame with MultiIndex.

If you like to unpivot tables you can check the resources below.

## Resources

- [reset\_index](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset%5Findex.html?ref=datascientyst.com)
- [to\_records](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to%5Frecords.html?highlight=to%5Frecords&ref=datascientyst.com)
- [How to Melt Pandas DataFrame - pd.melt in Examples](https://datascientyst.com/use-melt-pandas-dataframe-pd-melt-examples/)
- [Opposite of Melt in Python and Pandas](https://datascientyst.com/opposite-of-melt-python-pandas/)
- [How To Create a Pivot Table in Pandas?](https://datascientyst.com/how-to-create-a-pivot-table-in-pandas/)
- [How to Flatten a MultiIndex in Pandas](https://datascientyst.com/flatten-multiindex-in-pandas/)