valueError: Length mismatch: Expected axis has 40 elements, new values have 38 elements


I take the original model and prepare to run a new dataset. Of course, the columns will be different
the new dataset does not have a head, so I’m stupid to count a number of columns (it should be wrong. My original number of 38 is actually 40)
and report valueerror: length mismatch: expected axis has 40 elements, new values have 38 elements error

Solution ideas

My English is poor, so Google translated what this sentence means
valueerror: length mismatch: the expected axis has 40 elements and the new value has 38 elements

That is to say, I counted wrong
in order to prove it, I must output. The shape result is (100000, 40) OK, then I’ll recognize it and change it

Data processing and analysis

Before getting the dataset, you’d better take a shape to see how many rows and columns


Get (10000, 40)

For a dataset without a head, you need to define a header name

train_df.columns=['click']+['f'+str(i) for i in range(39)]
features=['f'+str(i) for i in range(39)]

It’s that simple

I write 39 because the target value in front of me has this

Error reason

It’s mainly because you can’t count.
it’s clearly said that there are 40 columns, so don’t be stubborn. Only 38 columns will be finished. Small mistakes are very simple

Read More: