I'm trying to assign one of 8 labels to my data based on the strings in an existing column. However, with the method I'm using I get this error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I have 144 different strings I'm looking for, that I want to assign to 8 labels.
Here is a simplified example of what I mean. If A is the existing column in my dataframe, I want to create B with the strings assigned depending on the value of A.
Dataframe:
A B
0 1 low
1 1 low
2 2 mid
3 3 mid
4 5 high
5 4 mid
6 2 mid
7 5 high
The code I'm using currently is something like:
for index, row in df.iterrows():
if df['A'] == 1:
df['Label'] = 'low'
elif any([df['A'] == 2, df['A'] == 3, df['A'] == 4]):
df['Label'] = 'mid'
elif df['A'] == 5:
df['Label'] = 'high'
I think it is the use of any() that is giving me the error. As I understand it, this is because of how pandas works, but I don't really understand it. Is there any easier way to do this?
Any help or pointers would be appreciated :)