0

can I choose a specific string of a column to become NaN in python? My data frame shows like this:

type    size
A        1
B        1  
C        1

and I want to convert 'B' become Nan, so the table will be like:

type    type
A        1
NaN      1
C        1

thankyou.

yangyang
  • 491
  • 4
  • 16

1 Answers1

0

You can use np.where

df['type'] = np.where(df.type.eq('B'), np.nan, df.type)

You can also use df.loc

df.loc[df.type.eq('B'), 'type'] = np.nan

Output:

  type  size
0    A     1
1  NaN     1
2    C     1
deadshot
  • 8,881
  • 4
  • 20
  • 39