最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Melting multiple columns in Python Panda - Stack Overflow

matteradmin5PV0评论

Melting multiple columns in Python Panda

Hello all,

I stack in this coding for two day. I want to get a proper df for EDA and then machine learning. I am new to Python. Could you help?

My data

enter image description here

After pivot

enter image description here

tourism5 = tourism4.pivot_table(index=["Year"], columns=["Tourists to Ireland", "Area of Residence"], values="VALUE")

tourism5

tourism6 = tourism5.melt(id_vars=["Year"], var_name= ["Average Stay Nights", "Tourists Expenditure", "Tourists Number"],value_name="VALUE")

tourism6

I try to melt it. But, it keeps giving error message. Thanks for your help in advance.

File C:\Users\anaconda3\Lib\site-packages\pandas\core\reshape\melt.py:74, in melt(frame, id_vars, value_vars, var_name, value_name, col_level, ignore_index)70 if missing.any():71     missing_labels = [72         lab for lab, not_found in zip(labels, missing) if not_found73     ]---> 74     raise KeyError(75         "The following id_vars or value_vars are not present in "76         f"the DataFrame: {missing_labels}"77     )78 if value_vars_was_not_none:79     frame = frame.iloc[:, algos.unique(idx)]
KeyError: "The following id_vars or value_vars are not present in the DataFrame: ['Year']"
Post a comment

comment list (0)

  1. No comments so far