1

I used the code below to map the 2 values inside S column to 0 but it didn't work. Any suggestion on how to solve this? N.B : I want to implement an external function inside the map.

 df = pd.DataFrame({
   'Age': [30,40,50,60,70,80],
   'Sex': ['F','M','M','F','M','F'],
   'S'  : [1,1,2,2,1,2]
 })
 def app(value):
     for n in df['S']:
         if n == 1:
             return 1
         if n == 2:
             return 0
 df["S"] = df.S.map(app)
mit
  • 11,083
  • 11
  • 50
  • 74

8 Answers8

2

Don't use apply, simply use loc to assign the values:

df.loc[df.S.eq(2), 'S'] = 0

   Age Sex  S
0   30   F  1
1   40   M  1
2   50   M  0
3   60   F  0
4   70   M  1
5   80   F  0

If you need a more performant option, use np.select. This is also more scalable, as you can always add more conditions:

df['S'] = np.select([df.S.eq(2)], [0], 1)
user3483203
  • 50,081
  • 9
  • 65
  • 94
2

Use eq to create a boolean series and conver that boolean series to int with astype:

df['S'] = df['S'].eq(1).astype(int)

OR

df['S'] = (df['S'] == 1).astype(int)

Output:

   Age Sex  S
0   30   F  1
1   40   M  1
2   50   M  0
3   60   F  0
4   70   M  1
5   80   F  0
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
1

You're close but you need a few corrections. Since you want to use a function, remove the for loop and replace n with value. Additionally, use apply instead of map. Apply operates on the entire column at once. See this answer for how to properly use apply vs applymap vs map

def app(value):
    if value == 1:
        return 1
    elif value == 2:
        return 0
df['S'] = df.S.apply(app)
   Age Sex  S
0   30   F  1
1   40   M  1
2   50   M  0
3   60   F  0
4   70   M  1
5   80   F  0
W Stokvis
  • 1,409
  • 8
  • 15
0

You can do:

import numpy as np

df['S'] = np.where(df['S'] == 2, 0, df['S'])
rahlf23
  • 8,869
  • 4
  • 24
  • 54
0

If you only wish to change values equal to 2, you can use pd.DataFrame.loc:

df.loc[df['S'] == 0, 'S'] = 0

pd.Series.apply is not recommend and this is just a thinly veiled, inefficient loop.

jpp
  • 159,742
  • 34
  • 281
  • 339
0

You could use .replace as follows: df["S"] = df["S"].replace([2], 0) This will replace all of 2 values to 0 in one line

0

Go with vectorize numpy operation:

df['S'] = np.abs(df['S'] - 2)

and stand yourself out from competitions in interviews and SO answers :)

Patrick the Cat
  • 2,138
  • 1
  • 16
  • 33
0
>>>df = pd.DataFrame({'Age':[30,40,50,60,70,80],'Sex': 
 ['F','M','M','F','M','F'],'S': 
 [1,1,2,2,1,2]})


>>> def app(value):
        return 1 if value == 1 else 0 
    # or app = lambda value : 1 if value == 1 else 0

>>> df["S"] = df["S"].map(app)

>>> df 
   Age  S Sex
      Age  S Sex
   0   30  1   F
   1   40  1   M
   2   50  0   M
   3   60  0   F
   4   70  1   M
   5   80  0   F
dimension
  • 982
  • 10
  • 18