Home » How to Fix: ValueError: All arrays must be of the same length

How to Fix: ValueError: All arrays must be of the same length

by Erma Khan

One error you may encounter when using pandas is:

ValueError: All arrays must be of the same length

This error occurs when you attempt to create a pandas DataFrame and not every column in the DataFrame has the same length.

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we attempt to create the following pandas DataFrame:

import pandas as pd

#define arrays to use as columns in DataFrame
team = ['A', 'A', 'A', 'A', 'B', 'B', 'B']
position = ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F']
points = [5, 7, 7, 9, 12, 9, 9, 4]

#attempt to create DataFrame from arrays
df = pd.DataFrame({'team': team,
                   'position': position,
                   'points': points})

ValueError: All arrays must be of the same length

We receive an error that tells us each array does not have the same length.

We can verify this by printing the length of each array:

#print length of each array
print(len(team), len(position), len(points))

7 8 8

We see that the ‘team’ array only has 7 elements while the ‘position’ and ‘points’ arrays each have 8 elements.

How to Fix the Error

The easiest way to address this error is to simply make sure that each array we use has the same length:

import pandas as pd

#define arrays to use as columns in DataFrame
team = ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B']
position = ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F']
points = [5, 7, 7, 9, 12, 9, 9, 4]

#create DataFrame from arrays
df = pd.DataFrame({'team': team,
                   'position': position,
                   'points': points})

#view DataFrame
df

	team	position points
0	A	G	 5
1	A	G	 7
2	A	F	 7
3	A	F	 9
4	B	G	 12
5	B	G	 9
6	B	F	 9
7	B	F	 4

Notice that each array has the same length this time.

Thus, when we use the arrays to create the pandas DataFrame we don’t receive an error because each column has the same length.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes

Related Posts