Issue
Here I am applying OneHotEncoder to one of my dataframe columns.
dfcars= pd.read_excel('cars.xlsx')
ohe=OneHotEncoder()
temp1= pd.DataFrame(ohe.fit_transform(dfcars[['Car Model']]).toarray())
ohe.categories_
dfcars = pd.concat([dfcars,temp1], axis=1)
This is my dataset after aplying OHE: dfcars
dfcars[0]
doesn't display the the first column.
dfcars[4]
shows error.
Why is this happening?
Solution
We can access any column using the column name df['column_name']
and the same thing is happening here with df[0]
as one of the column created after applying OneHotEncoder
is named as 0
.
To slice the dataframe using index values, one can use iloc
.
df.iloc[:,:1]
can be used to access the first column.
Answered By - Hari Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.