Home > Mobile >  Python pandas DataFrame Columns and Data passed seem differ in size, but no
Python pandas DataFrame Columns and Data passed seem differ in size, but no

Time:02-26

I am using Python3.9 and pandas.

I am trying to build a DataFrame, but during execution I get this error:

raise ValueError(err) from err
       ValueError: 13 columns passed, passed data had 14 columns
import pandas as pd
from pandas.core.frame import DataFrame

When I do this:

df_printers = pd.DataFrame(printers, columns=[
            'id_printer',
            'serial_number',
            'product_number',
            'installation_date',
            'product_full_name',
            'country_code',
            'customer_name',
            'sold_to_customer_name'
            'warranty_ship_to_country_code',
            'warranty_active',
            'warranty_start_date',
            'warranty_end_date',
            'number_active_contracts',
            'number_active_care_packs',
        ])

I am trying to build the DataFrame, the objects "printers" is a list of printers where the element of the list has 14 fields, and I am passing "columns" that has, again, 14 fields.

But at executing the code I get the error.

enter image description here

printers is this:

printers = []

printers.append([
                        str(serial_number)   "@"   str(product_number),
                        serial_number,
                        product_number,
                        installation_date,
                        product_full_name,
                        country_code,
                        customer_name,
                        sold_to_customer_name,
                        warranty_ship_to_country_code,
                        warranty_active,
                        warranty_start_date,
                        warranty_end_date,
                        number_active_contracts,
                        number_active_care_packs
                    ])

As you can see, it has 14 columns/fields.

I really don't get the error, the number of the columns/fields, in both cases, is 14.

But it's telling me that I am passing elements of 14 and instead defining a DataFrame with 13 columns.

I use a Virtual Environment in Windows and Python 3.9.


Variables inspections with PyCharm: printers is a list of 5 elements. Printers list

Let's inspect one element of the list. It must have 14 columns.

First Element of the list

Yes! It has 14 columns/field like the DataFrame requires !

so why I get this error ?

Please, before put negative feedback to this question, give me suggestions on how to improve. Many thanks to everybody.

CodePudding user response:

You are missing a trailing comma in your column list.

columns = [
            'id_printer',
            'serial_number',
            'product_number',
            'installation_date',
            'product_full_name',
            'country_code',
            'customer_name',
            'sold_to_customer_name'
            'warranty_ship_to_country_code',
            'warranty_active',
            'warranty_start_date',
            'warranty_end_date',
            'number_active_contracts',
            'number_active_care_packs',
        ]

print(columns)

['id_printer',
 'serial_number',
 'product_number',
 'installation_date',
 'product_full_name',
 'country_code',
 'customer_name',
 'sold_to_customer_namewarranty_ship_to_country_code',
 'warranty_active',
 'warranty_start_date',
 'warranty_end_date',
 'number_active_contracts',
 'number_active_care_packs']

CodePudding user response:

If I understand correctly, I think your issue is you are appending a list to a list, therefore creating a list of lists of length 1, even though you have 14 elements in the nested list.

There are a few ways to handle this. You can go into the list item by list indexing, i.e. printer[0], and access the data you intended.

Another way it can be handled is to use list comprehension to unpack the list when appending. Something like this:

_ = [printers.append(thing) for thing in someList]

or

printers = [thing for thing in someList]

You may also want to consider creating a separate list variable instead of doing everything inside the append method.

  • Related