I am using pandas 1.14.
I have dataframe that looks like this:
col1 col2 ....
A B C D E
11 1 1 1 1 2 3
3 3 4
30 3 10 2
... ...
22 3 4 5 6 3 1
df.index outputs
MultiIndex([('11', '1', '1', '1', '1'),
('11', '1', '1', '1', '3'),
('11', '1', '1', '30', '3'),
...
('22', '3', '4', '5', '6')],
names=["A","B","C", "D", "E"], length=10000)
df.columns outputs
Index(["col1", "col2", ...], dtype="object")
what I want to do it add both columns and divide by 2. in single index dataframe I would usually do
df["new"] = (df["col1"] + df["col2"])/2
how can I do this with multiindex dataframe?
My desired dataframe should look like this
col1 col2 new
A B C D E
11 1 1 1 1 2 3 2.5
3 3 4 3.5
30 3 10 2 6
... ...
22 3 4 5 6 3 1 2
Thanks in advance!
df["new"] = (df["col1"] + df["col2"])/2not work? I thought it would. Could you explain the error you get when you try?