ValueError: All arrays must be of the same length ( Solved )

ValueError_ All arrays must be of the same length ( Solved )

You may get errors like ValueError: All arrays must be of the same length while using the pandas.DataFrame() constructor in python. In this article you will learn how to solve the error ValueError: All arrays must be of the same length easily.

Why ValueError: All arrays must be of the same length occurs?

In most cases, you will get this error when there is a mismatch in the number of rows for each column in the dataframe. For example, In two columns of data, one column has five values and the other column has only three data points. Now if you will use the pd.DataFrame() on it then you will get the error All arrays must be of the same length.

See the error after executing the below lines of code.

import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3]}
df = pd.DataFrame(data)
print(df)

Output

arrays must be of the same length error in two columns
arrays must be of the same length error in two columns

You can see the number of elements in col2 is not the same as in col1.  Also, You will get the same error if you have more than two columns and there is a mismatch between the length of the records.

import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3,4,5],"col3":[100,110]}
df = pd.DataFrame(data)
print(df)

Output

arrays must be of the same length error in three columns
arrays must be of the same length error in three columns

Solution of All arrays must be of the same length

The solution to this valueError is very simple. You have to make sure that the length of each column must be the same. Either you remove the columns that are causing this error or add some records on them to match the length of the other columns.

You will not get the error if I remove the third column.

import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3,4,5]}
df = pd.DataFrame(data)
print(df)

Output

 col1 col2
0 10 1
1 20 2
2 30 3
3 40 4
4 50 5

Similarly, if you will add some records on the third column then you will not get the error.

import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3,4,5],"col3":[100,110,120,130,140]}
df = pd.DataFrame(data)
print(df)

Output

 col1 col2 col3
0 10 1 100
1 20 2 110
2 30 3 120
3 40 4 130
4 50 5 140

Conclusion

Always make sure that the length of each column of your dataset must be the same if you want to convert it into the dataframe for avoiding the All arrays must be of the same length error. The above methods will solve your error easily if you are getting that error. More on Valueerror for concept building.

ValueError: zero-dimensional arrays cannot be concatenated ( Solved )

valueerror: can only compare identically-labeled dataframe objects

I hope you have liked this tutorial. If you are still not able to solve this error then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner