22

I am trying to create a pandas dataframe with one row using and ended up testing the following simple line of code:

df = pd.DataFrame([1,2,3,4], columns=['a', 'b', 'v', 'w'])

Although this seems very simple i get the following error

Shape of passed values is (1, 4), indices imply (4, 4)

I have seen a few answers on this issues but all provide a way around it that doesn't explain why this happens and does not apply in my case.

Thanks is advance.

saias
  • 406
  • 1
  • 3
  • 12

1 Answers1

36

you need to change the list of values to [[1,2,3,4]]

df = pd.DataFrame([[1,2,3,4]], columns=['a', 'b', 'v', 'w'])
nimrodz
  • 1,504
  • 1
  • 13
  • 18
  • 3
    I cannot i believe I lost so much time for such a simple thing! Thanks lot! – saias Jun 15 '18 at 11:00
  • 2
    Its still not clear to me "why this happens". I don't have such a straight-forward initialization, but a similar error message. What indices is the error message referring to? Why wouldn't the OP init result in a 1x4 df? Neither `[1,2,3,4]` nor `['a', 'b', 'v', 'w']` appears to imply a 4x4 shape, so why would you need to force it into such a shape by nesting braces? – topher217 Aug 08 '21 at 09:37