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'))