1

I am trying to convert string values in a column to binary values.For example in the below table - the mapping will be like this {'cinema':0, 'education':1}.

enter image description here

I have defined a small function as below -

def numconv(a):
return a.map({'education' : 1,'cinema' : 0})

and then I have used 'apply' function to update the values in the dataframe -

train_docs['Class'] = train_docs['Class'].apply(numconv)

But I am getting error as below -

enter image description here

What I am doing wrng here?It should work as per my understanding.

Rajib Biswas
  • 47
  • 1
  • 5

2 Answers2

1

you can use map -

a = {'education' : 1,'cinema' : 0}
train_docs['Class'] = train_docs['Class'].map(a)
Nk03
  • 14,699
  • 2
  • 8
  • 22
0

When you use .apply method of pandas.Series given function should accept what that pandas.Series is holding, in this case strs,

def numconv(a):
    return a.map({'education' : 1,'cinema' : 0})

will not work if a is str, you might repair it by changing to

def numconv(a):
    return {'education' : 1,'cinema' : 0}[a]

or in this case just use .replace method of pandas.Series like so

import pandas as pd
df = pd.DataFrame({"Class":["education","education","education","cinema","cinema"]})
df["Class"].replace({'education' : 1,'cinema' : 0},inplace=True)
print(df)

output

   Class
0      1
1      1
2      1
3      0
4      0

Beware that function I proposed will fail if any other value will appears, whilst .replace ignores unknown values

Daweo
  • 31,313
  • 3
  • 12
  • 25