I have a pandas DataFrame like this:
import pandas as pd
import numpy as np
data1 = np.repeat(np.array(range(3), ndmin=2), 3, axis=0)
columns1 = pd.MultiIndex.from_tuples([('foo', 'a'), ('foo', 'b'), ('bar', 'c')])
df1 = pd.DataFrame(data1, columns=columns1)
print(df1)
foo bar
a b c
0 0 1 2
1 0 1 2
2 0 1 2
And another one like this:
data2 = np.repeat(np.array(range(3, 5), ndmin=2), 3, axis=0)
columns2 = ['d', 'e']
df2 = pd.DataFrame(data2, columns=columns2)
print(df2)
d e
0 3 4
1 3 4
2 3 4
Now, I would like to replace 'bar' of df1 with df2, but the regular syntax of single-level indexing doesn't seem to work:
df1['bar'] = df2
print(df1)
foo bar
a b c
0 0 1 NaN
1 0 1 NaN
2 0 1 NaN
When what I would like to get is:
foo bar
a b d e
0 0 1 3 4
1 0 1 3 4
2 0 1 3 4
I'm not sure if I'm missing something on the syntax or if this is related to the issues described here and here. Could someone explain why this doesn't work and how to get the desired outcome?
I'm using python 2.7 and pandas 0.24, if it makes a difference.