简体   繁体   中英

How to append MultiIndex rows to empty pandas dataframe

I'd like to do something like this:

df = pd.DataFrame()
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           

and get out:

     6 7 8
0 3  x x x
  4  x x x
  5  x x x
1 3  x x x
  4  x x x
  5  x x x
2 3  x x x
  4  x x x
  5  x x x

(As a bonus, winner gets to fill in the answers.)

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(
    levels=[[], []], codes=[[], []]
))
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM