1

I have a pandas df with various columns. One column - myCol - looks like this:

df

someCol   myCol
a         [{}]
b         [{'X': {'A': "value", 'B': "value"}}]
c         [{}, {}]
d         [{'X': {'A': "value", 'B': "value", 'C': "value"}}]

The maximum number of key-val pairs in X is unknown: some rows contain them all, some only contain a selection, and some are empty. I would like to replace myCol with actual columns, with as many columns as needed depending on the unique number of key-val pairs in X. So in this particular example, I would end up with:

df

someCol   A       B       C
a         N/A     N/A     N/A
b         value   value   N/A     
c         N/A     N/A     N/A
d         value   value   value

I am struggling in coming up with a general way to solve this, which is needed since I don't know how many 'additional' columns I will need in the end. Any ideas would be much appreciated.

1

1 Answer 1

2

Solution return first lists and dictionary with key X, then convert Nones to empty dicts and last pass to DataFrame constructor:

d = [{} if pd.isna(x) else x for x in df.pop('myCol').str[0].str.get('X')]
df = df.join(pd.DataFrame(d, index=df.index))
print (df)
  someCol      A      B      C
0       a    NaN    NaN    NaN
1       b  value  value    NaN
2       c    NaN    NaN    NaN
3       d  value  value  value
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. If the values are True or False instead of "value", how can I adapt your answer to that?
@CHRD - I think no change.
True, that didn't matter. Thanks alot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.