Skip to content

pandas-use-of-dot-stack (PD013)

Derived from the pandas-vet linter.

What it does

Checks for uses of .stack on Pandas objects.

Why is this bad?

Prefer .melt to .stack, which has the same functionality but with support for direct column renaming and no dependence on MultiIndex.

Example

import pandas as pd

cities_df = pd.read_csv("cities.csv")
cities_df.set_index("city").stack()

Use instead:

import pandas as pd

cities_df = pd.read_csv("cities.csv")
cities_df.melt(id_vars="city")

References