-1

I have a dataframe that I am using melt that gives me a ValueError in pandas2. Any idea why I get this. This code works fine in older versions though

from pandas import DataFrame
data = DataFrame({'A': ['a1', 'a2', 'a3'], 'B': ['b1', 'b1', 'b2'], 'C':[1,2,3]})
data.melt(id_vars=['A', 'B'], value_vars='C', var_name='var_C', value_name='C')

This gives me an error

ValueError: value_name (C) cannot match an element in the DataFrame columns.

If I use a different value_name than C then it works fine

data.melt(id_vars=['A', 'B'], value_vars='C', var_name='var_C', value_name='C1')

Are there any restrictions on value_name?

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
bhamu
  • 121
  • 6
  • It used to be a future warning in older version, so yes you will have restrictions on the value_name in the latest pandas version ```FutureWarning: This dataframe has a column name that matches the 'value_name' column name of the resulting Dataframe. In the future this will raise an error, please set the 'value_name' parameter of DataFrame.melt to a unique name.``` – Suraj Shourie Aug 22 '23 at 16:01
  • @SurajShourie If this is an explicit restriction, why isn't it mentioned in the documentation of `pd.melt()`? Should this be reported as a documentation bug? – Barmar Aug 23 '23 at 16:24
  • It is not on the documentation, and can be reported. the source code for [melt](https://github.com/pandas-dev/pandas/blob/v2.0.3/pandas/core/reshape/melt.py#L38-L158) does raise a ValueError when ```value_name``` is in ```dataframe.columns``` – Suraj Shourie Aug 23 '23 at 17:17
  • raised an issue @Barmar – Suraj Shourie Aug 23 '23 at 17:23

0 Answers0