pandas 按名称将几组列融为多个目标列 [英] Pandas Melt several groups of columns into multiple target columns by name

查看:82
本文介绍了 pandas 按名称将几组列融为多个目标列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个数据框的几组列融合为多个目标列.类似于问题 Python Pandas熔化初始列的组分为多个目标列 pandas数据框重塑/将多个值变量堆叠到单独的列中.但是,我需要按列名而不是按索引位置来明确地做到这一点.

I would like to melt several groups of columns of a dataframe into multiple target columns. Similar to questions Python Pandas Melt Groups of Initial Columns Into Multiple Target Columns and pandas dataframe reshaping/stacking of multiple value variables into seperate columns. However I need to do this explicitly by column name, rather than by index location.

import pandas as pd
df = pd.DataFrame([('a','b','c',1,2,3,'aa','bb','cc'), ('d', 'e', 'f', 4, 5, 6, 'dd', 'ee', 'ff')],
                  columns=['a_1', 'a_2', 'a_3','b_1', 'b_2', 'b_3','c_1', 'c_2', 'c_3'])
df

原始数据框:

    id   a_1  a_2  a_3  b_1  b_2  b_3  c_1  c_2  c_3
0   101   a    b    c    1    2    3    aa   bb   cc
1   102   d    e    f    4    5    6    dd   ee   ff

目标数据框

     id   a   b   c
0   101   a   1   aa
1   101   b   2   bb
2   101   c   3   cc
3   102   d   4   dd
4   102   e   5   ee
5   102   f   6   ff

对此方法的建议非常赞赏.

Advice is much appreciated on an approach to this.

推荐答案

有一种更有效的方法来解决涉及熔化多组不同列的这类问题. pd.wide_to_long是为这些确切情况而构建的.

There is a more efficient way to do these type of problems that involve melting multiple different sets of columns. pd.wide_to_long is built for these exact situations.

pd.wide_to_long(df, stubnames=['a', 'b', 'c'], i='id', j='dropme', sep='_')\
  .reset_index()\
  .drop('dropme', axis=1)\
  .sort_values('id')

    id  a  b   c
0  101  a  1  aa
2  101  b  2  bb
4  101  c  3  cc
1  102  d  4  dd
3  102  e  5  ee
5  102  f  6  ff

这篇关于 pandas 按名称将几组列融为多个目标列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆