Is it possible to end up with a categorical variable column after a melt operation in pandas?
If I set up the data like this:
import pandas as pd
import numpy as np
df = pd.DataFrame(
np.random.randn(3, 5),
columns=["A", "B", "C", "D", "E"]
)
df["id"] = range(1, 4)
df
| | A | B | C | D | E | id |
|----|-----------|-----------|-----------|-----------|------------|------|
| 0 | -0.406174 | -0.686917 | -0.172913 | -0.273074 | -0.0246714 | 1 |
| 1 | 0.323783 | -1.7731 | 1.57581 | -1.15671 | -1.23926 | 2 |
| 2 | -1.1426 | -0.591279 | 1.15265 | 0.326712 | -0.86374 | 3 |
and then apply
melted_df = df.melt(id_vars="id", value_vars=["A", "B", "C", "D", "E"])
melted_df
| | id | variable | value |
|----|------|------------|------------|
| 0 | 1 | A | -0.406174 |
| 1 | 2 | A | 0.323783 |
| 2 | 3 | A | -1.1426 |
| 3 | 1 | B | -0.686917 |
| 4 | 2 | B | -1.7731 |
| 5 | 3 | B | -0.591279 |
| 6 | 1 | C | -0.172913 |
| 7 | 2 | C | 1.57581 |
| 8 | 3 | C | 1.15265 |
| 9 | 1 | D | -0.273074 |
| 10 | 2 | D | -1.15671 |
| 11 | 3 | D | 0.326712 |
| 12 | 1 | E | -0.0246714 |
| 13 | 2 | E | -1.23926 |
| 14 | 3 | E | -0.86374 |
The dtype of the variable column is object
melted_df.dtypes
id int64
variable object
value float64
dtype: object
I'd like this to be category. I know, I can convert it easily by:
melted_df["variable"].astype("category")
But for large datasets, I'd like to avoid this overhead. In the documentation I didn't find such an option, but since the resulting column contains categorical data by definition, I presume there must be a possiblity.