2

I am trying to apply lambda with if-else condition on a pandas df df looks like following:

col1 col2 col3 col4 <---column names
None None None col4 <---column values in str
col1 None None None
None col2 None None



df_twitter_archive_master[['col1','col2','col3','col4']].apply(lambda x: x=0 if x=='None' else x=1)

basically, it should replace 'None' value with 0 and else with 1 but I keep getting this error

df_twitter_archive_master[['col1','col2','col3','col4']].apply(lambda x: x=0 if x=='None' else x=1)

SyntaxError: invalid syntax

^ is under x=1

what am i doing wrong? ?

ikel
  • 1,790
  • 6
  • 31
  • 61

2 Answers2

6

IIUC

df.replace('None',np.nan).notnull().astype(int)
Out[31]: 
   col1  col2  col3  col4
0     0     0     0     1
1     1     0     0     0
2     0     1     0     0

Base on your lambda method

df.applymap(lambda x: 0 if x=='None' else 1)
Out[33]: 
   col1  col2  col3  col4
0     0     0     0     1
1     1     0     0     0
2     0     1     0     0
BENY
  • 317,841
  • 20
  • 164
  • 234
  • what is difference between .apply and .applymap ?? – ikel Feb 16 '18 at 03:59
  • @ikel you can check https://stackoverflow.com/questions/19798153/difference-between-map-applymap-and-apply-methods-in-pandas – BENY Feb 16 '18 at 04:01
  • i see, i just tested with 'else 1' it works, but when I remove else 1, it got same syntax error? – ikel Feb 16 '18 at 04:09
  • @ikel `DataFrame.apply` passes each column (default, axis=0) or row (axis=1) to the provided function. `applymap` operates on the individual elements within the rows and columns. `Series.apply` operates on the elements of the series. `Series.map` and `Series.apply` are subtly different in the general case, but are effective the same when passed a callable function. – Paul H Feb 16 '18 at 05:37
5

Let's try:

(df != 'None').astype(int)

Output:

   col1  col2  col3  col4
0     0     0     0     1
1     1     0     0     0
2     0     1     0     0
Scott Boston
  • 147,308
  • 15
  • 139
  • 187