-1

I have a dataframe which looks like this after I split and expanded it using .str.split(' ', 40, expand=True) to a whole physical address for each row.

0       1   2             3        4        5           flag
1255    N   Camino        AVE      1071     San Diego   1     
1255    N   DelSur        AVE      1191     San Diego   1     
3642    E   CONSTITUTION  DR    San Diego   CA          0

Now, I want to write a conditional function which overwrites column[4]. I want to say if flag==1 then replace column[4] values with whitespace else keep the value from column[4]. This is example the output should look like this:

0       1   2             3        4        5           flag
1255    N   Camino        AVE               San Diego   1     
1255    N   DelSur        AVE               San Diego   1     
3642    E   CONSTITUTION  DR    San Diego   CA          0

However, in my case, it spits out an error. Here is my code:
df[4] = ['' if i=='1' else addresses[4] for i in df['flag']]

**The output is an error: RecursionError: maximum recursion depth exceeded while calling a Python object**

Can someone help please?

Lroy_12374
  • 67
  • 6

2 Answers2

0

We usually do np.where

df[4] = np.where(df['flag']==1, '', df[4])
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Use

df.loc[df.flag == 1, '4'] = ''
Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17