[英]Combining Excel Files with MultiIndexed Columns in Pandas
我正在處理一些原始數據並創建了 3 個單獨的數據框,我將它們導出到 excel。 這些數據幀之一是帶有行的常規列,另外兩個是帶有行的多索引列。 我想將所有三列組合成一個數據框(並排列)。 我試過在 axis=1 (列)上連接,返回的數據框丟失了一些列名(由於不同大小的索引)。 我不能使用合並,因為它們是三個沒有共享列的獨立數據框。
Below are samples of what these 3 dataframes look like separately.
df1 = | A | B |
|----------|----------|
| 10 | 11 |
| 20 | 21 |
df2= | X |
|---------------------|
| C | D |
|----------|----------|
| 5 | 6 |
| 30 | 9 |
df3 = | Y |
|---------------------|
| J | K |
|--------- |--------- |
|Q | R |
|----------|----------|
| 10 | 11 |
| 20 | 21 |
I would like to be able to have the above side by side and export it in a single dataframe. Any advice?
As mentioned earlier - I tried pd.concatenate and it deleted some of the column names from the multiindex dataframes and merge is not possible due to not having a shared column.
選項 1:前置 MultiIndex 級別
您可以將空的 MultiIndex 級別添加到df1
和df2
,以便級別在所有數據幀中匹配。
# Prepare session
import pandas as pd
# Recreate data
df1 = pd.DataFrame({'A':[10, 20], 'B':[11, 21]})
df2 = pd.DataFrame({'C': [5, 30], 'D': [6, 9]})
df2.columns = pd.MultiIndex.from_arrays([('X', 'X'), ('C', 'D')])
df3 = pd.DataFrame({'Q': [10, 20], 'R': [11, 21]})
df3.columns = pd.MultiIndex.from_arrays([('Y', 'Y'), ('J', 'K'), ('Q', 'R')])
# Adding empty multi-indices to match df3
df1 = pd.concat([df1], keys=[('', '')], axis=1)
df2 = pd.concat([df2], keys=[('')], axis=1)
# Combine data frames
df = pd.concat([df1, df2, df3], axis=1)
選項 2:降低 MultiIndex 級別
您可以刪除除最后一個 MultiIndex 級別以外的所有級別。
# Drop Multiindex Levels
df2.columns = df2.columns.get_level_values(-1)
df3.columns = df3.columns.get_level_values(-1)
# Combine data frame
df = pd.concat([df1, df2, df3], axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.