panda-melt 没有将列转置为序列

panda-melt is not transposing columns into a sequance

我读了一个 csv,它有多个列。我要转置

Date                A        B            C
   25/5/2019       25      765.3        896
   26/5/2019       98                  769 
   27/5/2019      27.6    453.2       98.6

我试过了

df2 = pd.melt(df, id_vars=['DateTime'],value_vars=['A','B','C'],
                  var_name='detail',value_name='')
df2 = df2.sort_values(['DateTime'])

它正在将列更改为行,但我遇到了两个问题。

  1. 它还在添加列名以及我不想要的值。我只需要值

  2. 没有列的顺序。我想按顺序转置col。

当前输出为

Date            final values        
 25/5/2019      A 25                
 25/5/2019      C 896                    
 25/5/2019       B 765.3                  
 26/5/2019       B                    
 26/5/2019       A 253  
 26/5/2019       C 769  
 27/5/2019       C 506

我想转置序列中的 col,总是从 A 开始,然后是 B,然后是 C。预期输出是。

Date            final values        
 25/5/2019       25                
 25/5/2019       765.3                    
 25/5/2019       896                   
 26/5/2019       98                    
 26/5/2019       null 
 26/5/2019       769
 27/5//2019      203

如有任何帮助,我们将不胜感激。提前致谢

An additional point I have now required output column final value. I need to assign that each value belongs to which column for this I have code for three columns. for example A=100, B=200, C=100. Is there any way that I can print codes next to each columns value

Date            final values    code       
 25/5/2019       25              100  
 25/5/2019       765.3           200         
 25/5/2019       896             300      
 26/5/2019       98              100      
 26/5/2019       null            200
 26/5/2019       769              300
 27/5//2019      203             100

DataFrame.stack and removeinf second level from columns names by DataFrame.droplevel 的替代解决方案:

df = (df.set_index('DateTime')[['A','B','C']]
        .stack(dropna=False)
        .droplevel(1)
        .reset_index(name='final values'))
print (df)
    DateTime  final values
0  25/5/2019          25.0
1  25/5/2019         765.3
2  25/5/2019         896.0
3  26/5/2019          98.0
4  26/5/2019           NaN
5  26/5/2019         769.0
6  27/5/2019          27.6
7  27/5/2019         453.2
8  27/5/2019          98.6

您可以使用底层数组进行灵活的整形:

pd.DataFrame({'Date': df['Date'].repeat(df.shape[1]-1),
              'final values': df.set_index('Date').to_numpy().ravel()})

或:

pd.DataFrame({'Date': df['Date'].repeat(df.shape[1]-1),
              'final values': df[['A', 'B', 'C']].to_numpy().ravel()})

输出:

        Date final values
0  25/5/2019         25.0
0  25/5/2019        765.3
0  25/5/2019        896.0
1  26/5/2019         98.0
1  26/5/2019          NaN
1  26/5/2019        769.0
2  27/5/2019         27.6
2  27/5/2019        453.2
2  27/5/2019         98.6

前两个答案的混合版本 melt。您只需在 melt 之前转置您的数据框以保持您的值有序:

out = (
    df.set_index('Date').T.melt(value_name='final value', ignore_index=False) \
      .assign(code=lambda x: x.index.map({'A': 100, 'B': 200, 'C': 300}))
      .reset_index(drop=True)
)
print(out)

# Output
        Date final value  code
0  25/5/2019        25.0   100
1  25/5/2019       765.3   200
2  25/5/2019       896.0   300
3  26/5/2019        98.0   100
4  26/5/2019         NaN   200
5  26/5/2019       769.0   300
6  27/5/2019        27.6   100
7  27/5/2019       453.2   200
8  27/5/2019        98.6   300