ValueError: Shape of passed values is (2, 1), indices imply (24, 1)

I have a dataset which contains two columns of “Time” and “traffic”. I’m trying to use windowing when feeding data to LSTM, using this code:

df=df.iloc[:,1:3]
vek=pd.DataFrame() # Create an empty DataFrame named 'vek'
yr=np.array([]) # Create an empty NumPy array named 'yr'
for i in range(2004,2006): 
    for j in range(1,13): # Loop through the months 1 to 12
        Time=str(i)+"_"+str(j) # Create a string in the format "year_month"
        yr=np.append(yr,[Time])
tefe=np.array(df.iloc[:1,:3]) #Extracts the first row and the first 2 columns of the DataFrame df and converts it into a NumPy array called tefe

for k in range(2005,2006):
    m=k-2004
    n=k-2003
    tefe=np.concatenate([tefe,np.array(df.iloc[m:n,:3])],axis=1)
    
yr=yr.T #Transposes the 'yr' array.
cf = pd.DataFrame(tefe.T, columns = ["Yearly Traffic"], index = yr) #Creates a new Pandas DataFrame named cf using the transposed tefe array. 

cf=cf.dropna() #Drops any rows in the DataFrame cf that contain missing values.
print(cf)

and it gives me the following error: ValueError: Shape of passed values is (2, 1), indices imply (24, 1)

could you tell me please how to resolve this error?

Hi @Narsis, The error occurs when the shape of the values passed to pandas data frame does not match the shape of the indices (or columns).

For example, i want to create a data frame using values of shape (3,4)

import pandas as pd
import numpy as np

arr = np.array([
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
])

print(arr.shape) #OutPut(3, 4)

but if i define only 3 columns while defining the data frame

df = pd.DataFrame(arr, columns=['A', 'B', 'C'])

then it will give ValueError: Shape of passed values is (3, 4), indices imply (3, 3)

The column names should be equal to the number of columns of the values.

 df = pd.DataFrame(arr, columns=['A', 'B', 'C', 'D])

this will work fine. Thank You.