samedi 26 janvier 2019

How to group 2 unique variables in a column of a data frame for creating dummy variables in python

i want to group 2 unique variables such that that i will be able to generate only one dummy variable having common frequency of both of them i tried this:

 for i in range(len(c_all['V14'])):
        if c_all['V14'][i] == '1' :
           print(i)
           c_all.loc[i,'V14'] = '0'
        elif c_all['V14'][i] == '2':
            c_all.loc[i,'V14'] = '0'

but i get this error:

TypeError                                 Traceback (most recent call last)
<ipython-input-301-860e1e51a315> in <module>()
      1 for i in range(len(c_all['V14'])):
----> 2     if c_all['V14'][i] == '1' :
      3         print(i)
      4         c_all.loc[i,'V14'] = '0'
      5     elif c_all['V14'][i] == '2':

E:\Anaconda\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
   1251 
   1252             with np.errstate(all='ignore'):
-> 1253                 res = na_op(values, other)
   1254             if is_scalar(res):
   1255                 raise TypeError('Could not compare {typ} type with Series'

E:\Anaconda\lib\site-packages\pandas\core\ops.py in na_op(x, y)
   1164                     result = method(y)
   1165                 if result is NotImplemented:
-> 1166                     raise TypeError("invalid type comparison")
   1167             else:
   1168                 result = op(x, y)

TypeError: invalid type comparison
-----------------------------------

can someone suggest me how to proceed?

Aucun commentaire:

Enregistrer un commentaire