Last updated: Apr 11, 2024
Reading time·4 min
DataFrame
or a Series
The Pandas "ValueError: Length of values (X) does not match length of index
(Y)" occurs when you try to assign a column of a NumPy array of a different
length to a DataFrame
.
To solve the error, use a Series
instead of a NumPy array.
Here is an example of how the error occurs.
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) # A B C # 0 1 4 7 # 1 2 5 8 # 2 3 6 9 print(df) df['D'] = [10, 11] # ⛔️ ValueError: Length of values (2) does not match length of index (3) print(df)
The error is also caused if you try to assign a column that points to a NumPy array of a different length.
import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) # A B C # 0 1 4 7 # 1 2 5 8 # 2 3 6 9 print(df) df['D'] = np.array([10, 11]) # ⛔️ ValueError: Length of values (2) does not match length of index (3) print(df)
The NumPy array only has 2 elements but each column in the DataFrame
has 3
rows, so it is expected that the NumPy array should also have 3 elements.
You can solve the error by using a pandas Series
instead of a list or a NumPy
array.
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) df['D'] = pd.Series([10, 11]) # A B C D # 0 1 4 7 10.0 # 1 2 5 8 11.0 # 2 3 6 9 NaN print(df)
The
pandas.Series
class is used to create a one-dimensional ndarray
with axis labels.
However, notice that we have a NaN
value because the Series
only has 2
elements whereas each column in the DataFrame
has 3 rows.
If you need to replace the NaN
values in the DataFrame
with zeros, use the
DataFrame.fillna()
method.
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) df['D'] = pd.Series([10, 11]) df = df.fillna(0) # A B C D # 0 1 4 7 10.0 # 1 2 5 8 11.0 # 2 3 6 9 0.0 print(df)
The DataFrame.fillna() method fills NA/NaN with the supplied value.
You can also pass the inplace
argument when calling fillna()
to avoid
reassigning the variable.
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) df['D'] = pd.Series([10, 11]) df.fillna(0, inplace=True) # A B C D # 0 1 4 7 10.0 # 1 2 5 8 11.0 # 2 3 6 9 0.0 print(df)
If the inplace
argument is set to True
, then the DataFrame
is filled
in-place.
When the argument is set to True
, the original DataFrame
object is mutated
and None
is returned.
DataFrame
or a Series
The error also occurs when you incorrectly try to create a DataFrame
or a
Series
.
Here is an example.
import pandas as pd # ⛔️ ValueError: Length of values (2) does not match length of index (3) df = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4], 'C': [5, 6] }, index=['X', 'Y', 'Z'])
Each column in the DataFrame
has 2 rows (values), however, we specified 3
values in the index
list.
To solve the error, we have to only specify 2 values in the index
list or add
a row to each column (A
, B
and C
).
import pandas as pd df = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4], 'C': [5, 6] }, index=['X', 'Y']) # A B C # X 1 3 5 # Y 2 4 6 print(df)
Now each column in the DataFrame
has 2 rows and the index contains 2 items, so
the issue is resolved.
You might also encounter the error when creating a Series
.
import pandas as pd # ⛔️ ValueError: Length of values (3) does not match length of index (2) series = pd.Series( [1, 2, 3], name='ser', index=['A', 'B'] )
The list we passed to the pandas.Series()
constructor contains 3 elements,
however, the index
list only contains 2.
To solve the error, make sure the index
list also has 3 elements.
import pandas as pd series = pd.Series( [1, 2, 3], name='ser', index=['A', 'B', 'C'] ) # A 1 # B 2 # C 3 # Name: ser, dtype: int64 print(series)
Now the list we passed to the pandas.Series
class contains 3 elements and the
index
list also contains 3 elements, so everything works as expected.
You can learn more about the related topics by checking out the following tutorials: