簡體   English   中英

如何使用 pandas 堆棧/熔體重塑多索引 dataframe

[英]How to reshape a multiindex dataframe using pandas stack/melt

*我正在編輯這個,希望讓它更清楚。 我正在嘗試利用堆棧和熔化的組合將此示例 dataframe 轉換為以下所需的 output。

使用 pandas 我在下面的 excel 表中加載,最終目標是使其整潔和長。

 df=pd.read('myfile.xlsx)
  df1= df.stack(level=1)
   df2=df1.melt(col_level =1)

樣品 dataframe

良好的客戶服務 新鮮食材 很多座位
爸爸約翰斯 分段平均 爸爸約翰斯 分段平均 爸爸約翰斯 分段平均
2019 年 12 月 1 日 70 88 2019 年 12 月 1 日 2.2 5.5 2019 年 12 月 1 日 5.2 8.8
2019 年 12 月 2 日 50 78 2019 年 12 月 2 日 6.8 4.4 2019 年 12 月 2 日 5.3 7.8
2019 年 12 月 3 日 60 77 2019 年 12 月 3 日 8.9 2.3 2019 年 12 月 3 日 6.3 5.6
2019 年 12 月 4 日 30 76 2019 年 12 月 4 日 7.3 7.3 2019 年 12 月 4 日 7.9 4.6

這是我正在尋找的最終結果

日期 餐廳 問題 分數
2019 年 12 月 1 日 棒棒糖 良好的客戶服務 70
2019 年 12 月 2 日 棒棒糖 良好的客戶服務 50
2019 年 12 月 3 日 棒棒糖 良好的客戶服務 60
2019 年 12 月 4 日 棒棒糖 良好的客戶服務 30
2019 年 12 月 4 日 棒棒糖 新鮮食材 2.2
2019 年 12 月 4 日 棒棒糖 新鮮食材 6.8
2019 年 12 月 4 日 棒棒糖 新鮮食材 8.9
2019 年 12 月 4 日 棒棒糖 新鮮食材 7.3

假設您的 excel 文件如下所示:

在此處輸入圖像描述

以下是我將如何使用 Pandas 處理它的步驟:

  1. 讀取文件但跳過前兩行並刪除空列
  2. 將 DF 轉換為多索引 DF
  3. 使用for loop熔化並連接 DF
# Step 1
df = pd.read_excel("raw_data.xlsx", skiprows=2, header=None)
df.dropna(axis=1, inplace=True)


# Step 2(a): Create the multi-index column name
col_name = np.array(['date', 'Papa Johns', 'Segment Avg'])
category = ['Good Customer Service', 'Fresh Ingredients', 'Lots of Seating']
outer = [cat for cat in category for _ in range(3)]
inner = [col for _ in range(3) for col in col_name]


# Step 2(b): Change the DF into Multi-index
df.columns = [outer, inner]
print(df)

 Good Customer Service                        Fresh Ingredients             \
                   date Papa Johns Segment Avg              date Papa Johns   
0            2019-01-12         70          88        2019-01-12        2.2   
1            2019-02-12         50          78        2019-02-12        6.8   
2            2019-03-12         60          77        2019-03-12        8.9   
3            2019-04-12         30          76        2019-04-12        7.3   

              Lots of Seating                         
  Segment Avg            date Papa Johns Segment Avg  
0         5.5      2019-01-12        5.2         8.8  
1         4.4      2019-02-12        5.3         7.8  
2         2.3      2019-03-12        6.3         5.6  
3         7.3      2019-04-12        7.9         4.6


# Step 3
final_df = pd.DataFrame(columns=['date', 'question', 'restaurant', 'score']) #Empty DF

for cat in category:
    temp_df = df.melt(id_vars=[(cat, 'date')],
                      value_vars=[(cat, 'Papa Johns')])
    temp_df.columns = ['date', 'question', 'restaurant', 'score']
    final_df = pd.concat([final_df, temp_df])
    
print(final_df)

        date               question  restaurant score
0 2019-01-12  Good Customer Service  Papa Johns    70
1 2019-02-12  Good Customer Service  Papa Johns    50
2 2019-03-12  Good Customer Service  Papa Johns    60
3 2019-04-12  Good Customer Service  Papa Johns    30
0 2019-01-12      Fresh Ingredients  Papa Johns   2.2
1 2019-02-12      Fresh Ingredients  Papa Johns   6.8
2 2019-03-12      Fresh Ingredients  Papa Johns   8.9
3 2019-04-12      Fresh Ingredients  Papa Johns   7.3
0 2019-01-12        Lots of Seating  Papa Johns   5.2
1 2019-02-12        Lots of Seating  Papa Johns   5.3
2 2019-03-12        Lots of Seating  Papa Johns   6.3
3 2019-04-12        Lots of Seating  Papa Johns   7.9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM