Data Wrangling with Python Datatable - Replicate Pandas’ Map Function#
Link to Source data#
Task: Create a new column by mapping the values in the city
column to the city_state
dictionary
from datatable import dt, f
df = dt.Frame({
"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"],
"last_name": ["Miller", "Jacobson", "Ali", "Milner", "Cooze"],
"age": [42, 52, 36, 24, 73],
"city": ["San Francisco", "Baltimore", "Miami", "Douglas", "Boston"],
})
city_to_state = {
"San Francisco": "California",
"Baltimore": "Maryland",
"Miami": "Florida",
"Douglas": "Arizona",
"Boston": "Massachusetts",
}
df
first_name | last_name | age | city | |
---|---|---|---|---|
▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | |
0 | Jason | Miller | 42 | San Francisco |
1 | Molly | Jacobson | 52 | Baltimore |
2 | Tina | Ali | 36 | Miami |
3 | Jake | Milner | 24 | Douglas |
4 | Amy | Cooze | 73 | Boston |
city_to_state
{'San Francisco': 'California',
'Baltimore': 'Maryland',
'Miami': 'Florida',
'Douglas': 'Arizona',
'Boston': 'Massachusetts'}
Complete Solution#
m = df["city"]
m.replace(city_to_state)
df["state"] = m
df
first_name | last_name | age | city | state | |
---|---|---|---|---|---|
▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | |
0 | Jason | Miller | 42 | San Francisco | California |
1 | Molly | Jacobson | 52 | Baltimore | Maryland |
2 | Tina | Ali | 36 | Miami | Florida |
3 | Jake | Milner | 24 | Douglas | Arizona |
4 | Amy | Cooze | 73 | Boston | Massachusetts |
Breakdown of Solution#
Step 1: Create a temporary dataframe to hold the values in city
.
m = df['city']
m
city | |
---|---|
▪▪▪▪ | |
0 | San Francisco |
1 | Baltimore |
2 | Miami |
3 | Douglas |
4 | Boston |
Step 2: Replace the values in m
with city_to_state
, by using the replace function. Note that the replace function does not require assignment, as the computation is done inplace:
m.replace(city_to_state)
m
city | |
---|---|
▪▪▪▪ | |
0 | California |
1 | Maryland |
2 | Florida |
3 | Arizona |
4 | Massachusetts |
Step 3: Assign m
to new column state
in df
:
df["state"] = m
df
first_name | last_name | age | city | state | |
---|---|---|---|---|---|
▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | ▪▪▪▪ | |
0 | Jason | Miller | 42 | San Francisco | California |
1 | Molly | Jacobson | 52 | Baltimore | Maryland |
2 | Tina | Ali | 36 | Miami | Florida |
3 | Jake | Milner | 24 | Douglas | Arizona |
4 | Amy | Cooze | 73 | Boston | Massachusetts |
Comments#