小编Mar*_*rov的帖子

在 Python Pandas 中同时融化多个列

想知道 pd.melt 是否支持熔化多个列。我有以下示例试图将 value_vars 作为列表列表,但出现错误:

ValueError: Location based indexing can only have [labels (MUST BE IN THE INDEX), slices of labels (BOTH endpoints included! Can be slices of integers if the index is integers), listlike of labels, boolean] types

使用熊猫 0.23.1。

df = pd.DataFrame({'City': ['Houston', 'Austin', 'Hoover'],
                   'State': ['Texas', 'Texas', 'Alabama'],
                   'Name':['Aria', 'Penelope', 'Niko'],
                   'Mango':[4, 10, 90],
                   'Orange': [10, 8, 14], 
                   'Watermelon':[40, 99, 43],
                   'Gin':[16, 200, 34],
                   'Vodka':[20, 33, 18]},
                 columns=['City', 'State', 'Name', 'Mango', 'Orange', 'Watermelon', 'Gin', 'Vodka'])
Run Code Online (Sandbox Code Playgroud)

期望的输出:

      City …
Run Code Online (Sandbox Code Playgroud)

melt pandas valueerror

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

melt ×1

pandas ×1

valueerror ×1