如何重塑熊猫数据框仅线条并增加价值

时间:2019-11-22 09:40:44

标签: python pandas dataframe reshape

我将这段代码作为输入(我将针对多个数据帧执行多次操作,因此我的代码处于循环状态)


v="S"
df_a = pd.DataFrame({ 'Type1': ['A'],'Type2': ['B'],'Type3': ['C'],'Type4': ['D']})
df_a

Output : 

    Type1   Type2   Type3   Type4
0   A       B       C       D


我需要这个


Output : 

    V   Col     Val
0   S   Type1   A
1   S   Type2   B
2   S   Type3   C
3   S   Type4   D


谢谢

2 个答案:

答案 0 :(得分:0)

尝试一下:

v="S"
df_a = pd.DataFrame({ 'V': ['S'],'Col': ['Type1'],'Val': ['A']},
               { 'V': ['S'],'Col': ['Type2'],'Val': ['B']},
               { 'V': ['S'],'Col': ['Type3'],'Val': ['C']},
               { 'V': ['S'],'Col': ['Type4'],'Val': ['D']})


 print(df_a)

之后,我们使用命令df.append添加行,此处为示例: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html

答案 1 :(得分:0)

以下陈述将解决您的目的:

delta[0,1].set_lb(0.5) delta[0,1].set_ub(0.5) delta[1,0].set_lb(0.5) delta[1,0].set_ub(0.5)
df_a = df_a.melt()