简体   繁体   English

如何将多索引行附加到空的熊猫数据帧

[英]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:可以预先初始化具有 2 个级别的MultiIndex ,以允许loc设置按预期工作:

# 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 : 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 :尽管在 MultiIndex 和列上使用广播乘法和 numpy 来构建 DataFrame 并使用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 : 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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