-1

i have this code as input ( i will do this many times for multiple dataframe, so my code is in loop )


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


and i need this


Output : 

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


Thank you

2 Answers2

0

try this:

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)

After that us the command df.append to add rows, an example here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html

jgalaso
  • 22
  • 4
0

Following statement will solve your purpose:

df_a = df_a.melt()
df_a['V'] = 'S'