串联时将新级别添加到MultiIndex

时间:2018-08-15 17:56:58

标签: python pandas concatenation

将大小,列和索引相同但内容有所不同的一些数据帧合并在一起。生成新索引以使原始索引保持不变,但最外层的索引现在表示已连接的数据帧号的最佳方法是什么?

public void OnImageAvailable(ImageReader reader)
    {
        var image = reader.AcquireNextImage();
        owner.mBackgroundHandler.Post(new ImageSaver(image, file));
    }

    // Saves a JPEG {@link Image} into the specified {@link File}.
    private class ImageSaver : Java.Lang.Object, IRunnable
    {
        // The JPEG image
        private Image mImage;

        // The file we save the image into.
        private File mFile;

        public ImageSaver(Image image, File file)
        {
            if (image == null)
                throw new System.ArgumentNullException("image");
            if (file == null)
                throw new System.ArgumentNullException("file");

            mImage = image;
            mFile = file;
        }

        public void Run()
        {
            ByteBuffer buffer = mImage.GetPlanes()[0].Buffer;
            byte[] bytes = new byte[buffer.Remaining()];
            buffer.Get(bytes);
            using (var output = new FileOutputStream(mFile))
            {
                try
                {
                    output.Write(bytes);
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }
                finally
                {
                    mImage.Close();
                }
            }
        }
    }

我对索引和堆栈仍然不太满意,但是我认为那是必要的。任何帮助将不胜感激!

沙箱:

DataFrame A:
Idx1 | Idx2 || Col
0      0       'A'
       1       'B'
1      0       'C'
       1       'D'

DataFrame B:
Idx1 | Idx2 || Col
0      0       'E'
       1       'F'
1      0       'G'
       1       'H'

DataFrame AB:
Idx0 | Idx1 | Idx2 || Col
0      0      0       'A'
              1       'B'
       1      0       'C'
              1       'D'
1      0      0       'E'
              1       'F'
       1      0       'G'
              1       'H'

1 个答案:

答案 0 :(得分:2)

concatkeysnames参数一起使用:

pd.concat([A, B], keys=[0, 1], names=['Idx0'])

结果输出:

               Col
Idx0 Idx1 Idx2    
0    0    0      A
          1      B
     1    0      C
          1      D
1    0    0      E
          1      F
     1    0      G
          1      H