答案 0 :(得分:0)
这种方法怎么样?
import pandas as pd
df = pd.DataFrame({'ColA': ['A','B','C'], 'ColB': ['D','E','F' ],'ColC':['G','H','I'],'Name':['Rajish','Rohit','Rahim']})
print(df)
结果:
ColA ColB ColC Name
0 A D G Rajish
1 B E H Rohit
2 C F I Rahim
然后进行转换:
import pandas as pd
df = pd.DataFrame({'ColA': ['A','B','C'], 'ColB': ['D','E','F' ],'ColC':['G','H','I'],'Name':['Rajish','Rohit','Rahim']})
print(df)
结果:
df2 = (df.set_index(['Name'])
.stack(dropna=False))
print(df2)