0

Updating to get the correct answer: My data frame is in the format. Below is just the dummy data

A         B
'Animal'  'Tiger'
'Speed'   '1'
'Mammal'  'Yes'
'Animal'  'Elephant'
'Speed'   '10'
'Mammal'  'No'
'Animal'  'Leapord'
'Speed'   '30'
'Mammal'  'No'

Desired output

Animal   Speed Mammal
Tiger    1     Yes
Elephant 10    Yes
Leapord  30    yes

how can I achieve this?

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Suraj Sareen
  • 71
  • 1
  • 7

1 Answers1

2

You can achieve this like so:

import pandas as pd
d = {'A': ['col1','col2','col3'], 'B': ['val1', 'val2','val3']}
df = pd.DataFrame(data=d)
df = df.set_index("A")
df = df.T
print(df.head)

Output:

A  col1  col2  col3
B  val1  val2  val3
Nathan Thomas
  • 260
  • 1
  • 8
  • Thanks Nathan. However this is not what I want exactly. I have updated the description of my question, to reflect accurately what I am looking for. Thanks for your help though. – Suraj Sareen Jun 20 '20 at 21:46