1

So I have a list of arrays in Python: [[0, 1, 0, 1], [1, 0, 1, 1], [0, 1, 1, 1]]. I would like to make this list of arrays into a Pandas dataframe, with each array being a row. Is there a way to do this quickly and easily in Python? I tried values = np.split(values, len(values)) to split the list of arrays into multiple arrays (well, I tried). And then tried to create a dataframe with df = pd.DataFrame(values). But this is where my error came from. I got a "must pass 2-d input" error message. Any idea what I'm doing wrong and how to fix it? Or an easier way to go about this? Thanks!

4
  • 1
    But you show a tuple of lists right now... Commented Jul 25, 2017 at 19:07
  • 1
    Did df = pd.DataFrame([[0, 1, 0, 1], [1, 0, 1, 1], [0, 1, 1, 1]]) not work? Commented Jul 25, 2017 at 19:07
  • 1
    ...which would work fine in pd.DataFrame() Commented Jul 25, 2017 at 19:07
  • Are you sure you have imported in correctly ? ie import pandas as pd? pd.DataFrame() should work Commented Jul 25, 2017 at 19:11

1 Answer 1

5

No need to do all that splitting, etc. If you have it as a list of lists that is two dimensional (meaning all rows have the same number of elements), you can simply pass it to the DataFrame constructor:

data = [[0, 1, 0, 1], [1, 0, 1, 1], [0, 1, 1, 1]]
pd.DataFrame(data)

generating the expected:

>>> pd.DataFrame(data)
   0  1  2  3
0  0  1  0  1
1  1  0  1  1
2  0  1  1  1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.