Feral Cat

Building just because.
python

Length of Values Does Not Match Length of Index

I’m guessing you are dynamically creating Pandas DataFrame columns and came across the, “Length of values does not match length of index” error. The issues lies in that the previous columns you’ve added are not the same length as the most recent one you’ve attempted to add. Let’s recreate the problem. Here’s an article for pip installation for MACS if you don’t have pandas or pip.

import pandas as pd
df = pd.DataFrame()
arrayOne = [1,2,3,4]
arrayTwo = [1,2,3,4,5]
df['1'] = arrayOne
df['2'] = arrayTwo

Output:
ValueError: Length of values does not match length of index

It’s very easy to see in this example that the first array contains 4 elements and the second 5. These both need to contain the same number of elements for us to not get this error. If these arrays were too long for us to visually examine we could always use LEN to get the number of elements.

arrayOneLength = len(arrayOne)
arrayTwoLength = len(arrayTwo)
arrayOneLength == arrayTwoLength

Output:
False

If you’re dynamically creating an array to eventually tack on to a DataFrame and are getting this error, look for if statements not paired with an else.

nameArray = ['Bill','Bob','Sandra','Joe','Jill']
ageArray = [35,32,46,19,17]
df = pd.DataFrame()
canDrinkArray = []
for element in ageArray:
    if element >= 21:
        canDrinkArray.append(True)

df['name'] = nameArray
df['age'] = ageArray
df['canDrink'] = canDrinkArray

This is an example of how I most often encounter this error. This will result in three True elements in the canDrinkArray. but how would our machines be able to connect the data in the canDrinkArray with the rest of data frame by anything else other than row number? It can’t… so error. The following should work seamlessly.

nameArray = ['Bill','Bob','Sandra','Joe','Jill']
ageArray = [35,32,46,19,17]
df = pd.DataFrame()
canDrinkArray = []
for element in ageArray:
    if element >= 21:
        canDrinkArray.append(True)
    else:
        canDrinkArray.append(False)
df['name'] = nameArray
df['age'] = ageArray
df['canDrink'] = canDrinkArray

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *