0

I have a DataFrame, that contains text description and its sub fields column-wise.

like,

<table>
  <tr>
    <th>Text</th>
    <th>Col1</th>
    <th>Col2</th>
    <th>Col3</th>
  </tr>
  <tr>
    <td>Boy eating apple</td>
    <td>boy</td>
    <td>eating</td>
    <td>apple</td>
  </tr>
  <tr>
    <td>Boy riding bike</td>
    <td>boy</td>
    <td>riding</td>
    <td>bike</td>
  </tr>
  <tr>
    <td>Boy driving car</td>
    <td>boy</td>
    <td>driving</td>
    <td>car</td>
  </tr>
</table>
</html>

Here I have code to do fit and transform as below:

le = preprocessing.LabelEncoder()
le.fit(data[['col1','col2','col3']])
data = le.transform(data[['col1','col2','col3']])

The above code gives "['col2'] not in index" exception for multiple columns, whereas code working fine with single column.

What is the solution?

jofrev
  • 324
  • 3
  • 11
VIVEK PATEL
  • 23
  • 1
  • 7
  • 1
    so far as i know, it is not allowed to encode more than one columns at the same time – Andy Mar 16 '20 at 06:59
  • Shouldn't you transform the result of fit? I'm not sure, I'm still learning scikit-learn. – stan0 Mar 16 '20 at 07:32
  • It sounds like you should use the [OneHotEncoder](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html) instead – Swier Mar 16 '20 at 07:37

0 Answers0