Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Map columns in dataframe from list

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading