1

I have a DataFrame (df1) looking like this:

        0   
Height  175     
Weight  80

Now I want to add a row Age with the value 37 but not at the end (for this we have already a solution) but on Top of my DataFrame.

What I have yet tries is:

pd.DataFrame(np.insert(df1.values, 0, values=['n',5], axis=0)) but I can onyl get this to work without the 'n' string and then there is only one column left.

Anyone a better idea that solves my problem?

ruedi
  • 5,365
  • 15
  • 52
  • 88

2 Answers2

4

Use DataFrame contructor with DataFrame.append or concat:

df = pd.DataFrame([37], index=['Age'], columns=[0]).append(df)
print (df)
          0
Age      37
Height  175
Weight   80

Alternative solution:

df = pd.concat([pd.DataFrame([37], index=['Age'], columns=[0]), df])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Because I can't comment, what do you want to do with the values['n',5] ?

Hanggy
  • 25
  • 9