Как использовать метод pandas concat для объединения мультииндекса

Quick Answer: pandas concat multiindex

The pandas library provides a concat function that can be used to concatenate multiple DataFrames. To concatenate DataFrames with a multiindex, you can pass a list of DataFrames to the concat function and specify the keys parameter.

import pandas as pd

# Create sample DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', 'B1']})
df2 = pd.DataFrame({'A': ['A2', 'A3'], 'B': ['B2', 'B3']})

# Concatenate DataFrames with multiindex
concatenated = pd.concat([df1, df2], keys=['df1', 'df2'])


The resulting concatenated DataFrame will have a multiindex, where the first level corresponds to the specified keys and the second level corresponds to the original index of each DataFrame.

Детальный ответ

pandas concat multiindex

When working with pandas, there are times when you may need to combine multiple DataFrames or Series together. One way to achieve this is by using the pandas.concat() function. This function allows you to concatenate objects along a particular axis, such as rows or columns. In this article, we will focus on concatenating DataFrames with a multiindex.

Understanding Multiindex

In pandas, a multiindex is a way to represent higher-dimensional data in a tabular format. It allows you to have multiple levels of index on both the rows and columns. This can be particularly useful when dealing with hierarchical or structured datasets.

Let's start by creating two simple DataFrames with a multiindex:

import pandas as pd

# First DataFrame
data1 = {'A': [1, 2, 3],
         'B': [4, 5, 6]}
index1 = pd.MultiIndex.from_tuples([('X', 'a'), ('X', 'b'), ('Y', 'c')], names=['Group', 'Subgroup'])

df1 = pd.DataFrame(data1, index=index1)

# Second DataFrame
data2 = {'C': [7, 8, 9],
         'D': [10, 11, 12]}
index2 = pd.MultiIndex.from_tuples([('X', 'd'), ('Y', 'e'), ('Y', 'f')], names=['Group', 'Subgroup'])

df2 = pd.DataFrame(data2, index=index2)

print("DataFrame 1:")
print("\nDataFrame 2:")


DataFrame 1:
           A  B
Group Subgroup      
X     a        1  4
      b        2  5
Y     c        3  6

DataFrame 2:
           C   D
Group Subgroup      
X     d        7  10
Y     e        8  11
      f        9  12

In the above example, we have created two DataFrames, df1 and df2, with a multiindex consisting of two levels: 'Group' and 'Subgroup'. This is denoted by the hierarchical display of the index.

Concatenating DataFrames with a Multiindex

Now that we have our two DataFrames, we can concatenate them using the pandas.concat() function. When concatenating DataFrames with a multiindex, we need to specify the axis along which the concatenation should take place. In this case, we want to concatenate along the rows, so we set the axis parameter to 0.

Let's see how this is done:

df_concat = pd.concat([df1, df2], axis=0)

print("Concatenated DataFrame:")


Concatenated DataFrame:
           A    B    C     D
Group Subgroup                  
X     a      1  4.0  NaN   NaN
      b      2  5.0  NaN   NaN
Y     c      3  6.0  NaN   NaN
X     d    NaN  NaN  7.0  10.0
Y     e    NaN  NaN  8.0  11.0
      f    NaN  NaN  9.0  12.0

As you can see, the resulting DataFrame df_concat contains all the rows from both df1 and df2. Additionally, it creates a new level in the index to differentiate between the original DataFrames.

Dealing with Duplicate Index Values

In certain cases, you might have duplicate index values in your DataFrames. When concatenating, this can lead to unexpected results. To handle this, you can set the verify_integrity parameter of the pandas.concat() function to True. This will raise a ValueError if there are any duplicate index values.

Let's demonstrate this with an example:

# Create two DataFrames with duplicate index values
data3 = {'A': [4, 5, 6],
         'B': [7, 8, 9]}
index3 = pd.MultiIndex.from_tuples([('X', 'a'), ('Y', 'c'), ('Y', 'c')], names=['Group', 'Subgroup'])
df3 = pd.DataFrame(data3, index=index3)

df_concat_error = pd.concat([df1, df3], axis=0, verify_integrity=True)

This will result in the following error:

Traceback (most recent call last):
  File "", line 7, in 
    df_concat_error = pd.concat([df1, df3], axis=0, verify_integrity=True)
  File "", line 212, in concat
    raise ValueError(msg)
ValueError: Indexes have overlapping values: Index([('X', 'a'), ('Y', 'c')])

The ValueError indicates that there are overlapping values in the index, and the concatenation cannot be performed without ambiguity. The error message provides the details of the overlapping values.


In this article, we have explored how to concatenate DataFrames with a multiindex using the pandas.concat() function. We have seen how to create DataFrames with a multiindex and how to perform concatenation along the rows. We have also discussed how to handle duplicate index values when concatenating.

By understanding these concepts and using the appropriate techniques, you can effectively combine and manipulate multiindex DataFrames in pandas to suit your analytical needs.

Видео по теме

How to Create Multi-Index DataFrame in Pandas

How do I use the MultiIndex in pandas?

How To Use MultiIndex In Pandas For Hierarchical Data

Похожие статьи:

Как использовать метод pandas concat для объединения мультииндекса

Как изменить имена столбцов в pandas dataframe

pandas dataframe выбрать несколько столбцов: простой способ для работы с данными

📊 Последовательность pandas: среднее значение данных и методы его вычисления