如何使用 melt 将多个列名作为 val_vars 传递?

How do I pass multiple column names as val_vars using melt?

我有一个大型数据框(367 行 × 342 列),其中多个列的名称前缀相同。我正在努力使我们的代码更易于使用。

当前代码:

                   value_vars = "'Intensity 01_1',
 'Intensity 01_2',
 'Intensity 01_3',
 'Intensity 03_1',
 'Intensity 03_2',
 'Intensity 03_3',
 'Intensity 04_1',
 'Intensity 04_2',
 'Intensity 04_3',
 'Intensity 05_1',
 'Intensity 05_2',
 'Intensity 05_3',
 'Intensity 06_1',
 'Intensity 06_2',
 'Intensity 06_3',,
                    var_name="SampleMeas", value_name="SpecInt"
                               )

这是我尝试使用的内容,但出现错误“TypeError: unhashable type: 'list'”

valvarlist = [col for col in protstack if 'Intensity' in col], 
[col for col in protstack if 'iBAQ' in col], 
[col for col in protstack if 'LFQ intensity' in col]
#print(valvarlist)

test = pd.melt(protstack, id_vars="Majority protein IDs", 
                   value_vars = valvarlist,
                    var_name="SampleMeas", value_name="SpecInt"
                               )

我试过将 valvarlist 放在 [] 中,但我得到了同样的错误。当我检查 type(valvarlist) 时,我得到一个 tuble,它应该可以与 melt 一起使用。

为链条条件or创建列名列表:

alvarlist = [col for col in protstack if 
                      ('Intensity' in col) or ('iBAQ' in col) or ('intensity' in col)]

或将 str.contains 与列名一起使用,将 | 用于测试值的正则表达式或:

alvarlist = df.columns[df.columns.str.contains('Intensity|iBAQ|intensity')]

样本:

df = pd.DataFrame(1, columns=['Intensity1','iBAQ1','intensity4','intensity','ss'],
                   index=[0,1])
print (df)
   Intensity1  iBAQ1  intensity4  intensity  ss
0           1      1           1          1   1
1           1      1           1          1   1

protstack = df.columns
alvarlist = [col for col in protstack if 
                      ('Intensity' in col) or ('iBAQ' in col) or ('intensity' in col)]
print (alvarlist)
['Intensity1', 'iBAQ1', 'intensity4', 'intensity']

alvarlist = df.columns[df.columns.str.contains('Intensity|iBAQ|intensity')]
print (alvarlist)
Index(['Intensity1', 'iBAQ1', 'intensity4', 'intensity'], dtype='object')