I have the following DataFrame:
df = pd.DataFrame({
"One_X": [1.1, 1.1, 1.1],
"One_Y": [1.2, 1.2, 1.2],
"Two_X": [1.11, 1.11, 1.11],
"Two_Y": [1.22, 1.22, 1.22]
})
I want to split df.columns into a MultiIndex where:
- Level 0: "One", "Two"
- Level 1: "X", "Y"
I used the following code (which is also in the Pandas cookbook documentation):
df.columns = pd.MultiIndex.from_tuples([tuple(c.split("_")) for c in df.columns])
However, I get the following error:
AttributeError: 'tuple' object has no attribute 'split'
How can I fix this issue please?
Note: AI generated he same code too.
Thanks in advance for your help!