I have the following df I want to map. Postal code is an int type and I want create a region column with codes that fall under certain ranges to be mapped to specific regions for example
range(2000-2999):"NSW", range(3000-3999):"Victoria", range(4000-5000): "Something", 9013: "Kangaroo"
Postal Code Region
2000
2001
3001
4001
4999
9013
I did the below however, i got an error - "TypeError: argument of type 'int' is not iterable"
df['Region']=df['Postal Code'].apply(lambda x:next((v for k, v in postal.items() if x in k),0))
if you could please provide a better way to iterate through a column with all integers and map specific ranges of postal codes to certain regions that would be highly appreciated!
Thanks
I'd use pd.IntervalIndex
with map
:
s = pd.Series(['NSW', 'Victoria', 'Something', 'Kangaroo'],
index=pd.IntervalIndex.from_arrays([9013,2000,3000,4000],
[9013,2999,3999,5000],
closed='both'))
df['Region'] = df['Postal Code'].map(s)
print(df)
Output:
Postal Code Region
0 2000 Victoria
1 2001 Victoria
2 3001 Something
3 4001 Kangaroo
4 4999 Kangaroo
5 9013 NSW
The error occurred because you put the ,0
in the wrong parentheses.
df['Region']=df['Postal Code'].apply(lambda x:next((v for k, v in postal.items() if x in k)),0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.