![](/img/trans.png)
[英]Pandas - melt, stack, reshape or MultiIndex dataframe columns based on multiple attributes from the column name
[英]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 处理它的步骤:
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.