1

Suppose I have the dataframe pd.DataFrame({'a':nan, 'b':nan, 'c':{'a':1, 'b':2},{'a':4, 'b':7, 'c':nan}, {'a':nan, 'b':nan, 'c':{'a':6, 'b':7}}). I want to take the values from the keys in the dictionary in column c and parse them into keys a and b.

Expected output is:

 a b c 0 1 2 {'a':1, 'b':2} 1 4 7 nan 2 6 7 {'a':6, 'b':7} 

I know how to do this to create new columns, but that is not the task I need for this, since a and b have relevant information needing updates from c. I have not been able to find anything relevant to this task.

Any suggestions for an efficient method would be most welcome.

** EDIT **

The real problem is that I have the following dataframe, which I reduced to the above (in several, no doubt, extraneous steps):

a b c 0 nan nan [{'a':1, 'b':2}, {'a':6, 'b':7}] 1 4 7 nan 

and I need to have output, in as few steps as possible, as per

 a b c 0 1 2 {'a':1, 'b':2} 1 4 7 nan 2 6 7 {'a':6, 'b':7} 

Thanks!

1
  • 1
    Can you provide your expected output? I think this may be .fillna Commented Mar 5, 2019 at 15:46

2 Answers 2

1

This works:

def func(x): d = eval(x['c']) x['a'] = d['a'] x['b'] = d['b'] return x df = df.apply(lambda x : func(x), axis=1) 
Sign up to request clarification or add additional context in comments.

1 Comment

I used a variant on this. Worked like a charm.
0

How about this:

for t in d['c'].keys(): d[t] = d['c'][t] 

Here is an example:

>>> d = {'a': '', 'b': '', 'c':{'a':1, 'b':2}} >>> d {'a': '', 'b': '', 'c': {'a': 1, 'b': 2}} >>> d.keys() dict_keys(['a', 'b', 'c']) >>> d['c'].keys() dict_keys(['a', 'b']) >>> for t in d['c'].keys(): ... d[t] = d['c'][t] ... >>> d {'a': 1, 'b': 2, 'c': {'a': 1, 'b': 2}} >>> 

We can turn it into a function:

>>> def updateDict(dict, sourceKey): ... for targetKey in dict[sourceKey].keys(): ... dict[targetKey] = dict[sourceKey][targetKey] ... >>> d = {'a': '', 'b': '', 'c':{'a':1, 'b':2}} >>> updateDict(d, 'c') {'a': 1, 'b': 2, 'c': {'a': 1, 'b': 2}} >>> d = {'a': '', 'b': '', 'c':{'a':1, 'b':2, 'z':1000}} >>> updateDict(d, 'c') {'a': 1, 'b': 2, 'c': {'a': 1, 'b': 2, 'z': 1000}, 'z': 1000} >>> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.