Skip to main content
 首页 » 编程设计

python之将列标题转换为行值

2025年05月04日9lvdongjie

我有一个看起来像这样的数据框

       Wash_Month  Wash_Day 
 
0           3         2 
1           4         3 

我需要把它转换成这个

          Input              Value 
 
0     Wash_Month/Wash_Day     3,2 
1     Wash_Month/Wash_Day     4,3 

我尝试合并两列,但无法将列标题转换为行值

谢谢。

请您参考如下方法:

这是一种可爱的做法

pd.DataFrame(dict( 
    Input='/'.join(df), 
    Value=[*map(','.join, zip(*map(df.astype(str).get, df)))] 
)) 
 
                 Input Value 
0  Wash_Month/Wash_Day   3,2 
1  Wash_Month/Wash_Day   4,3