pandas 如何将特定列转换为熊猫中的行关联其他列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42292986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:00:33  来源:igfitidea点击:

How To transpose specific columns into rows in pandas associate other column value

pythonpandasnumpyanalyticstranspose

提问by MegaBytes

Hi I am trying to do transpose operation in pandas, but the condition is the value of one column should be associated with the transposed rows. The example given below will explain the better way: the data is looks like:

嗨,我正在尝试在 Pandas 中进行转置操作,但条件是一列的值应与转置的行相关联。下面给出的例子将解释更好的方法:数据看起来像:

A   1   2   3   4  51  52 53 54 
B   11  22  23  24 71  72 73 74 

The result I am trying to do like this:

我试图这样做的结果是:

A   1   51
A   2   52
A   3   53
A   4   54
B   11  71
B   22  72
B   23  73
B   24  74

In first row, the data is in single row, I want to transpose data from 1 to 4 with the value 'A' in other column. Can anyone suggest how can I do this??

在第一行中,数据在单行中,我想将数据从 1 转置为 4,另一列中的值为 'A'。谁能建议我该怎么做?

回答by jezrael

It seems you need meltor stack:

看来您需要meltstack

print (df)
   0   1   2   3   4
0  A   1   2   3   4
1  B  11  22  23  24

df1 = pd.melt(df, id_vars=0).drop('variable', axis=1).sort_values(0)
df1.columns = list('ab')
print (df1)
   a   b
0  A   1
2  A   2
4  A   3
6  A   4
1  B  11
3  B  22
5  B  23
7  B  24

df2 = df.set_index(0).stack().reset_index(level=1, drop=True).reset_index(name='a')
df2.columns = list('ab')
print (df2)
   a   b
0  A   1
1  A   2
2  A   3
3  A   4
4  B  11
5  B  22
6  B  23
7  B  24

EDIT by comment:

通过评论编辑:

#set index with first column
df = df.set_index(0)

#create MultiIndex
cols = np.arange(len(df.columns))
df.columns = [ cols // 4, cols % 4]
print (df)
    0               1            
    0   1   2   3   0   1   2   3
0                                
A   1   2   3   4  51  52  53  54
B  11  22  23  24  71  72  73  74

#stack, reset index names, remove level and reset index
df1 = df.stack().rename_axis((None, None)).reset_index(level=1, drop=True).reset_index()
#set new columns names
df1.columns = ['a','b','c']
print (df1)
   a   b   c
0  A   1  51
1  A   2  52
2  A   3  53
3  A   4  54
4  B  11  71
5  B  22  72
6  B  23  73
7  B  24  74