I am fairly new to Python and currently I am trying to build a function that searches for the first 2 digits of the elements in a column and if true, return the result with a new header such as region
For example,
Adres AreaCode Region
0 SArea 123191 A
1 BArea 122929 A
2 AArea 132222 B
I want the function to search for just the first 2 digits of the AreaCode which would give me the result of along with a new header of Region which classifies the Region based on the first 2 digits of the AreaCode. So in this case 12 would give me A and 13 would give me B
I already tried this
df.loc[df.AreaCode == 123191, 'Region'] = 'A'
and this worked for the entire AreaCode but I have no idea how to modify it so that I would be able to search based on the first 2 digits.
and I tried this
df.loc[df.AreaCode.str.contains == 12, 'Region' ] = 'A'
but it gives me the error:
AttributeError: Can only use .str accessor with string values,
which use np.object_ dtype in pandas
How do I fix this and thanks a lot for helping!