0

I'm trying to encode/transform string values in a data frame to numbers, and I get the error shown below.

I don't think the issue is related to the shape fo the array, it's the dashes in the strings (ie. 'G-FM' 'G-FM' 'G-ON') as you see in the actual data as part of the error message below

ValueError: Expected 2D array, got 1D array instead: array=['G-FM' 'G-FM' 'G-ON' ... 'G-IM' 'G-IM' 'G-IM']. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or >array.reshape(1, -1) if it contains a single sample.

Here is my code:

import pandas as pd
from sklearn.preprocessing import OrdinalEncoder

data_ref_val = pd.read_csv('/home/ec2-user/file9.csv',quotechar='"', sep='\t', header=0, encoding= 'utf-8')

data_ref_val.fillna(0,inplace=True)

ord_enc = OrdinalEncoder()

data_ref_val["primary_specialty_group__v"] = ord_enc.fit_transform(data_ref_val["primary_specialty_group__v"].astype(str))
4

0 回答 0