I have a column (dataframe) that has numerical values like the ones shown below:
| Number |
|---|
| 1 |
| 2 |
| 4 |
| 16 |
| 8 |
| 2 |
I have the next lists:
asia = [22, 23, 0, 1, 2, 3, 4, 5, 6, 7]
asia_europe = [8]
europe = [9, 10, 11, 12]
europe_america = [13, 14, 15, 16]
america = [17, 18, 19, 20, 21]
I want the numbers in the column to be replaced with their respective text that comes from the lists so that my dataframe looks like this:
| Old Column | New Column |
|---|---|
| 1 | asia |
| 2 | asia |
| 4 | asia |
| 16 | europe_america |
| 8 | asia_europe |
| 2 | asia |
I don't know if there is a way to do this using pandas or if I have to use a for loop.
CodePudding user response:
You need to build a dictionary to reference the region from the number. For that a dictionary comprehension is handy:
d = dict(asia = [22, 23, 0, 1, 2, 3, 4, 5, 6, 7],
asia_europe = [8],
europe = [9, 10, 11, 12],
europe_america = [13, 14, 15, 16],
america = [17, 18, 19, 20, 21])
d2 = {v:k for k,l in d.items() for v in l}
# {0: 'asia',
# 1: 'asia',
# ...
# 21: 'america'}
Then map the value to the regions:
df['New Column'] = df['Old Column'].map(d2)
