braj
braj

Reputation: 2695

Index Mismatch error converting numpy array to dataframes in pandas

I am having a numpy array of (200, 2000) and trying to convert it to a dataframe of 200 columns and 2000 rows.
I already have a list of column names that I would like to use in the dataframe, but I am struggling with an index error.

Code:

indexes = range(len(features_bow)+1)
features_bow_df = pd.DataFrame(features_bow, index=indexes,columns=features_bow.shape)
#features_bow.shape is the set column names that I have in the form of a list.

Error:

ValueError: Shape of passed values is (200, 2000), indices imply (2, 2000)

Any help would be appreciable.

Upvotes: 0

Views: 1148

Answers (1)

Stefan
Stefan

Reputation: 42885

This looks like your columns have the wrong dimensions. .shape will return a tuple with two elements given the shape of your features_bow, hence you are getting the error that the column input has only length 2 as opposed to 200. Just put your column names into a list as seems your intention and you'll be fine. See also here on how to achieve this via slicing.

Upvotes: 1

Related Questions