I'm trying to write an if statement in Python that iterates over the rows of a DataFrame. If that row meets one of several possible conditions, it does a calculation. My MWE is:
import numpy as np
import pandas as pd
coin = ['a','b','c'] # conditions
xlist = pd.DataFrame() # data
xlist['col1'] = np.array(['a','j','k','b','b','d','c','a']) # state
xlist['col2'] = np.array([1,2,2,1,2,0,1,3])
for ii in range(len(xlist)):
if any(coin in xlist['col1'][ii]):
# if state meets a condition, do calculation x
print('result 1')
else:
# if state doesn't meet condition, do calculation y
print('result 2')
I keep getting an error TypeError: 'in <string>' requires string as left operand, not list
so this isn't the correct Python way of doing it. What am I doing wrong with the any
statement? The output should be
result 1
result 2
result 2
result 1
result 1
result 2
result 1
result 1
CodePudding user response:
Use np.where
with Series.isin
:
In [846]: l = np.where(xlist['col1'].isin(coin), 'result 1', 'result 2')
In [847]: l # numpy array
Out[847]:
array(['result 1', 'result 2', 'result 2', 'result 1', 'result 1',
'result 2', 'result 1', 'result 1'], dtype='<U8')
In [848]: l.tolist() # list
Out[848]:
['result 1',
'result 2',
'result 2',
'result 1',
'result 1',
'result 2',
'result 1',
'result 1']
CodePudding user response:
I think your condition is wrong. Instead of any(coin in xlist['col1'][ii])
, try xlist['col1'][ii] in coin
:
for ii in range(len(xlist)):
if xlist['col1'][ii] in coin:
# if state meets a condition, do calculation x
print('result 1')
else:
# if state doesn't meet condition, do calculation y
print('result 2')