1

I have a df with multiple columns. One of my column is extra_type. Now i want to create a new column based on the values of extra_type column. For example

extra_type
NaN
legbyes
wides
byes

Now i want to create a new column with 1 and 0 if extra_type is not equal to wide then 1 else 0 I tried like this

df1['ball_faced'] = df1[df1['extra_type'].apply(lambda x: 1 if [df1['extra_type']!= 'wides'] else 0)]

It not working this way.Any help on how to make this work is appreciated expected output is like below

extra_type  ball_faced
NaN           1
legbyes       1
wides         0
byes          1
Learner
  • 335
  • 3
  • 16

2 Answers2

2

Note that there's no need to use apply() or a lambda as in the original question, since comparison of a pandas Series and a string value can be done in a vectorized manner as follows:

df1['ball_faced'] = df1.extra_type.ne('wides').astype(int)

Output:

  extra_type  ball_faced
0        NaN           1
1    legbyes           1
2      wides           0
3       byes           1

Here are links to docs for ne() and astype().

For some useful insights on when to use apply (and when not to), see this SO question and its answers. TL;DR from the accepted answer: "If you're not sure whether you should be using apply, you probably shouldn't."

constantstranger
  • 9,176
  • 2
  • 5
  • 19
  • 1
    I have checked both implementations with 4k rows: 0.03849387168884277 seconds vs 0.0052607059478759766 seconds. The OP wanted to figure out .apply() method, but your solution is 10x faster. – Sergey Sakharovskiy Dec 23 '22 at 00:15
1
 df['ball_faced'] = df.extra_type.apply(lambda x: x != 'wides').astype(int)
extra_type ball_faced
0 NaN 1
1 legbyes 1
2 wides 0
3 byes 1