2

My code:

import pandas as pd
import numpy as np

d = {'one':[1,1,1,1,1],'two':[2,2,2,2,2],'letter':['a','a','b','b','c']}
e = np.array(d)

df = pd.DataFrame(e)

Throws this error:

ValueError: Must pass 2-d input
jwpfox
  • 5,124
  • 11
  • 45
  • 42
Matthew
  • 115
  • 10
  • 3
    Pass it as a list i.e `df = pd.DataFrame(e.tolist())` – Bharath M Shetty Dec 30 '17 at 04:13
  • 1
    Have you read the documentation? https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html - Do you have a specific question about how that doco relates to what you are doing? – jwpfox Dec 30 '17 at 04:19
  • 1
    https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe – BENY Dec 30 '17 at 04:27

1 Answers1

2

You don't need the e=np.array(d). The data parameter in the dataframe constructor accepts properly formatted dictionaries.

Just use

df = pd.Dataframe(d)

Output:

  letter  one  two
0      a    1    2
1      a    1    2
2      b    1    2
3      b    1    2
4      c    1    2
Scott Boston
  • 147,308
  • 15
  • 139
  • 187