简体   繁体   中英

Make dictionary from a string inside a cell of Pandas data-frame

My Pandas data-frame ( df ) has some strings in a column ( m )

df = pd.DataFrame({"m": ['0 = absence\n1 = mild\n2 = moderate \n3 = severe','0 = no \n1 = yes\n2 = Not relevant','1: smoker\n2: ex-smoker (not smoked)'], "c": [1,1,1], "x":[5,3,6]})

This creates the following data-frame:

在此处输入图像描述

Now, I need to split each string in the column m first by \n and then by = or : , to make it a dictionary like this:

在此处输入图像描述

Is there any compact way of doing this?

Looks like here is map method needed:

import re

df.loc[:, 'm'] = df['m'].map(lambda x: dict(map(lambda y: re.split(' = |: ', y), x.split('\n'))))

Output wee be like:

                                                   m  c  x
0  {'0': 'absence', '1': 'mild', '2': 'moderate '...  1  5
1      {'0': 'no ', '1': 'yes', '2': 'Not relevant'}  1  3
2     {'1': 'smoker', '2': 'ex-smoker (not smoked)'}  1  6

If the key is required as a number, you can add additional line:

df.loc[:, 'm'] = df['m'].map(lambda x: {int(k): v for k, v in x.items()})

Output:

                                                   m  c  x
0  {0: 'absence', 1: 'mild', 2: 'moderate ', 3: '...  1  5
1            {0: 'no ', 1: 'yes', 2: 'Not relevant'}  1  3
2         {1: 'smoker', 2: 'ex-smoker (not smoked)'}  1  6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM