A MultiIndex
with 2 levels can be pre-initialised to allow setting with loc
to work as expected:
# Pre-initialise a MultiIndex
df = pd.DataFrame(index=pd.MultiIndex.from_arrays([[], []]))
for row_ind1 in range(3):
for row_ind2 in range(3, 6):
for col in range(6, 9):
entry = row_ind1 * row_ind2 * col
df.loc[(row_ind1, row_ind2), col] = entry
df
:
6 7 8
0 3 0.0 0.0 0.0
4 0.0 0.0 0.0
5 0.0 0.0 0.0
1 3 18.0 21.0 24.0
4 24.0 28.0 32.0
5 30.0 35.0 40.0
2 3 36.0 42.0 48.0
4 48.0 56.0 64.0
5 60.0 70.0 80.0
Although it's probably easier to just use broadcasted multiplication with numpy on the MultiIndex and columns to build the DataFrame and create the index and columns independently with MultiIndex.from_product
:
import numpy as np
import pandas as pd
idx = pd.MultiIndex.from_product([[0, 1, 2], [3, 4, 5]]).to_frame()
cols = np.array([6, 7, 8])
df = pd.DataFrame((idx[0] * idx[1]).to_numpy()[:, None] * cols,
index=idx.index,
columns=cols)
df
:
6 7 8
0 3 0 0 0
4 0 0 0
5 0 0 0
1 3 18 21 24
4 24 28 32
5 30 35 40
2 3 36 42 48
4 48 56 64
5 60 70 80