0

I'm trying to send a nested list to a dataframe like so:

import pandas as pd
import numpy as np

def save_data(data):

    df = pd.DataFrame(data=[data], columns=['Send/Collect', 'Hospital', 'Courier', 'Kit', 'Manufacturer'])


save_data([["One", "Two","Three", "Four", "Five"],
           ["One", "Two","Three", "Four", "Five"],
           ["One", "Two","Three", "Four", "Five"]])

However this pulls the Assertion Error as follows:

AssertionError: 5 columns passed, passed data had 3 columns

As seen on Git and another question I tried making the data into a numpy array as suggested yet this now returns this slightly more confusing error:

ValueError: Must pass 2-d input

In the real code, the list changes size with a fixed column size and so I'm not sure how to fix this!

Colleen
  • 143
  • 1
  • 3
  • 14

2 Answers2

2

For me working remove [] from DataFrame contructor if working with nested lists and maximal length of nested lists is same like number of columns (here 5):

def save_data(data):

    df = pd.DataFrame(data=data, columns=['Send/Collect', 'Hospital',
                                          'Courier', 'Kit', 'Manufacturer'])
    return df

L = [["One", "Two","Three", "Four", "Five"],
     ["One", "Two","Three", "Four", "Five"],
     ["One", "Two","Three", "Four", "Five"]]
df = save_data(L)
print (df)
  Send/Collect Hospital Courier   Kit Manufacturer
0          One      Two   Three  Four         Five
1          One      Two   Three  Four         Five
2          One      Two   Three  Four         Five

You can also create condition for check this:

def save_data(data):
    if max(len(x) for x in L) == 5:
        df = pd.DataFrame(data=data, columns=['Send/Collect', 'Hospital', 'Courier', 
                                              'Kit', 'Manufacturer'])

    return df

L = [["One", "Two","Three", "Four", "Five"],
     ["One", "Two","Three", "Four", "Five"],
     ["One", "Two","Three", "Four"]]

df = save_data(L)
print (df)
  Send/Collect Hospital Courier   Kit Manufacturer
0          One      Two   Three  Four         Five
1          One      Two   Three  Four         Five
2          One      Two   Three  Four         None
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

You remove the brackets from data, eg.

def save_data(data):

    df = pd.DataFrame(data=data, columns=['Send/Collect',
                                          'Hospital',
                                          'Courier',
                                          'Kit',
                                          'Manufacturer'])
    return df
piman314
  • 5,285
  • 23
  • 35