0

Suppose I have the following dataframe df:

Column 1                Column 2
 0010050                   0.0
 0145784                   4.0
 1586256                   0.0
 5542644                   5.0
 1482875                   0.0
 1587254                   1.0
 1561550                   0.0

I want to create a pandas equation that says, if the first three numbers of column 1 value are in the list ['155', '156','001'] *and* the last number of column 1 value is == 0 *and* Column 2 == 0.0, df['Column 1'].str[:6], else F.

Such that I have:

Column 1                Column 2      Column 3
 0010050                   0.0         001005
 0145784                   4.0            F
 1586256                   0.0            F
 5542644                   5.0            F
 1482875                   0.0            F
 1587254                   1.0            F
 1561550                   0.0         156155 

I have tried the following code but I get ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().:

df = df.assign(Column3 = lambda x: np.where( (x['Column2'] == '0.0') & (x['Column 1'].str[:3] in ['001','155', '156']) & (x['Column 1 '].str[-1]=='0'), x['Column1'].str.slice(stop=6),'F'))
PKumar
  • 10,971
  • 6
  • 37
  • 52
Moshe
  • 107
  • 1
  • 9
  • Have you looked up the error? Stabbing in the dark is not debugging... – Mad Physicist Mar 22 '21 at 07:42
  • 1
    Change `(x['Column 1'].str[:3] in ['001','155', '156'])` to `x['Column 1'].str[:3].isin(['001','155', '156'])` – jezrael Mar 22 '21 at 07:43
  • 1
    All together `df = df.assign(Column3 = lambda x: np.where( (x['Column2'] == '0.0') & x['Column 1'].str[:3].isin(['001','155', '156']) & (x['Column 1'].str[-1]=='0'), x['Column 1'].str.slice(stop=6),'F'))` – jezrael Mar 22 '21 at 07:49

0 Answers0