I’m working with the following DataFrame
JavaScript
x
12
12
1
0 NaN
2
1 {u'bphigh': u'120', u'bplow': u'70', u'weight'
3
2 NaN
4
3 {u'bphigh': 120, u'bplow': 60, u'weight': u'10...
5
4 NaN
6
7
13149 NaN
8
13150 {u'bphigh': u'110', u'bplow': u'60', u'weight'
9
13151 {u'bphigh': u'149', u'bplow': u'90', u'weight'
10
13152 {u'bphigh': u'113', u'bplow': u'69', u'weight'
11
13153 {u'bphigh': u'115', u'bplow': u'76', u'weight'
12
Consisting of parameters (bphigh
bplow
weight
) of type str
as follows
{u'bphigh': u'120', u'bplow': u'70', u'weight': u'84.8'}
I’d like to extract these parameters and their corresponding values to columns as shown below
JavaScript
1
5
1
bphigh bplow weight
2
0 11 22 31
3
1 42 52 61
4
2 72 82 91
5
I tried using the following pandas method which hasn’t really been consistent in extracting the parameters
vitals['vital'].str.extract(r"{u'bphigh':s*(w+)")
Is there a more efficient workaround in pandas or regex to this issue?
Please Advise
Advertisement
Answer
JavaScript
1
2
1
from ast import literal_eval
2
try:
JavaScript
1
7
1
df['vital']=df['vital'].astype(str).map(lambda x:literal_eval(x) if x!='nan' else float('NaN'))
2
3
#In the above code we are making the string values to actual dictionary via
4
#map() method we are iterating the values of 'vital' column and converting the
5
#string dictionary to actual dictionary via literal_eval() method and anonymous function
6
#skipping 'nan's' via if/else condition inside map() method
7
Finally:
JavaScript
1
6
1
out=pd.DataFrame(df['vital'].dropna().tolist())[['bphigh','bplow','weight']]
2
3
#In the code we are making Dataframe out of the dictionary values in
4
#'vital' columns by making list of values of 'vial' column and then we
5
#are selecting only these 3 columns ['bphigh','bplow','weight']
6
Now If you print out
you will get your desired output