如何将熊猫列动态转换为行

时间:2019-09-22 09:37:56

标签: python pandas

我在熊猫中有以下数据框

code   tank  noz_sale_cumsum  noz_1_sub  noz_2_sub   noz_1_avg   noz_2_avg  noz_1_flag  noz_2_flag 
123    1     1234             12         23          23.23       32.45      short       ok                
123    2     1200             13         53          33.13       22.45      excess      ok            

列(例如noz_1_sub, noz_2_sub, noz_1_avg, noz_2_avg, noz_1_flag and noz_2_flag)是动态生成的。 我想要的数据框如下。

code  tank  noz_no   noz_sale_cumsum  noz_sub   noz_avg   noz_flag
123   1     1        1234             12        23.23     short
123   1     2        1234             23        32.45     ok
123   2     1        1200             13        33.13     excess
123   2     2        1200             53        22.45     ok

我正在熊猫里追随。

first I am getting all dynamic columns in different arrays

cols_sub = [cols for cols in df.columns if re.search('noz_\d+_sub', cols)]
cols_avg = [cols for cols in df.columns if re.search('noz_\d+_avg', cols)]
cols_flag = [cols for cols in df.columns if re.search('noz_\d+_flag', cols)]

final_df = df.pivot_table(index=['code', 'tank', 'noz_sale_cumsum'], columns=[cols_sub, cols_avg, cols_flag], values=[]).reset_index()      

我不确定“值”列以及如何从noz列中提取数字并将其放在noz_no列下。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

您可以使用onplay将所有内容转换为行,然后使用var songname = new Howl({ src: ['mp3/songname.mp3'], onload: function() { UpdateTime(songname); }, onplay: function() { setInterval(function(){ UpdateTime(songname); },50); }/*, onend: function() { maybe end the interval here?... }*/ }); function UpdateTime(x){ let a = (x.seek()).toFixed(); let a2 = Math.floor(a / 60); let a1 = a - a2 * 60; let b = (x.duration()).toFixed(); let b2 = Math.floor(b / 60); let b1 = b - b2 * 60; document.getElementById("time").innerHTML=a2+":"+a1+" / "+b2+":"+b1; } 将某些行转换回列。

melt

enter image description here

pivot_table

enter image description here