I have a DF with column containing array of values as per below
| name | urls |
|---|---|
| bob | [https:/1, https:/2] |
| mary | [https:/1, https:/3] |
I would like to map the values within the array in column "urls" to a dictionary and replace the values to those from the dictionary. Example dictionary below
mapping_dictionary = {'https:/1': 'google',
'https:/2': 'yahoo',
'https:/3': 'bing'}
So end result would look something like this:
| name | urls |
|---|---|
| bob | [google, yahoo] |
| mary | [google, bing] |
Is it possible to do this using pandas.replace or pandas mapping?
Thanks in advance
CodePudding user response:
You can explode "urls" columns, map "mapping_dictionary", aggregate by the index to lists:
df['urls'] = df['urls'].explode().map(mapping_dictionary).groupby(level=0).agg(list)
Output:
name urls
0 bob [google, yahoo]
1 mary [google, bing]
CodePudding user response:
Let us try
df['value' = df.urls.map(lambda x : [*map(mapping_dictionary.get, x)])
0 [google, yahoo]
1 [google, bing]
Name: urls, dtype: object
CodePudding user response:
You can change the urls using apply and lambda, as follows:
import pandas as pd
df = pd.DataFrame({
'name': ['bob', 'mary'],
'urls': [['https:/1', 'https:/2'], ['https:/1', 'https:/3']]
})
m_dict = {
'https:/1': 'google',
'https:/2': 'yahoo',
'https:/3': 'bing'
}
df['urls'] = df['urls'].apply(lambda urls: [m_dict[url] for url in urls])
print(df)
# name urls
#0 bob [google, yahoo]
#1 mary [google, bing]
