I have a dataframe and I want to convert one column into conditions.
For example, here is a sample dataframe:
df=pd.DataFrame({'a':['>28','27','<26'],'b':['1','2','3']})
df
a b
0 >28 1
1 27 2
2 <26 3
I want to generate a series of if statements to get ideal b value:
if a > 28:
b=1
elif a=27:
b=2
elif a < 26:
b=3
How can I do that? In my dataframe, all of elements are stored as string.
I was trying to use the iloc()
function to select, but it can't deal with range conditions (>
)
If there an elegant way to do this? or do I have to manually type all the conditions?