I have a pandas dataframe.
is_active Device
True 1
False 2
I have a file called mapping.json
[
{
"description": "Desktop",
"deviceId": "1"
},
{
"description": "Smartphone",
"deviceId": "2"
},
{
"description": "Tablet",
"deviceId": "3"
}
]
I need to map Device with device id from mapping.json to get final result as:
is_active Device
True Desktop
False Smartphone
How can I achieve it using pandas.
I am doing:
with open('mapping.json', 'r') as file:
c_map = json.load(file)
output_df['Device'] = output_df['Device'].map(c_map)
It gives me error: TypeError: ‘list’ object is not callable
>Solution :
You need to rework cmap to be a dictionary:
cmap = {d['deviceId']: d['description'] for d in cmap}
Then map will work as expected.