0

I'm trying to filter my data frame by making separate columns to increase readability and usability.

Problem statement: Column "Editables" has a nested dictionary as the value I'm trying to create separate columns as per the key. For instance 'photo_repace' 'text_remove', 'text_add' has different columns.

The nested dictionary:

{
'photo': {
    'photo_replace': None,
    'photo_remove': None,
    'photo_add': None,
    'photo_effect': None,
    'photo_brightness': None,
    'background_color': None,
    'photo_resize': None,
    'photo_rotate': None,
    'photo_mirror': None,
    'photo_layer_rearrange': None,
    'photo_move': None
},
'text': {
    'text_remove': None,
    'text_add': None,
    'text_edit': None,
    'font_select': None,
    'text_color': None,
    'text_style': None,
    'background_color': None,
    'text_align': None,
    'text_resize': None,
    'text_rotate': None,
    'text_move': None,
    'text_layer_rearrange': None
}
}

Output

enter image description here

Code I've been using:

df["editables"] = df["editables"].apply(lambda x : dict(eval(x)))
df_edit = df["editables"].apply(pd.Series )

Output: enter image description here

0

2 Answers 2

2

Try this:

>>> dct = {
'photo': {
    'photo_replace': None,
    'photo_remove': None,
    'photo_add': None,
    'photo_effect': None,
    'photo_brightness': None,
    'background_color': None,
    'photo_resize': None,
    'photo_rotate': None,
    'photo_mirror': None,
    'photo_layer_rearrange': None,
    'photo_move': None
},
'text': {
    'text_remove': None,
    'text_add': None,
    'text_edit': None,
    'font_select': None,
    'text_color': None,
    'text_style': None,
    'background_color': None,
    'text_align': None,
    'text_resize': None,
    'text_rotate': None,
    'text_move': None,
    'text_layer_rearrange': None
}
}
>>> pd.DataFrame(dct).T
       photo_replace  photo_remove  ...  text_move  text_layer_rearrange
photo            NaN           NaN  ...        NaN                   NaN
text             NaN           NaN  ...        NaN                   NaN

[2 rows x 22 columns]
>>> pd.DataFrame(dct).T[['photo_replace', 'photo_remove', 'text_remove']]
       photo_replace  photo_remove  text_remove
photo            NaN           NaN          NaN
text             NaN           NaN          NaN

NOTE: .T transposes the dataframe.

Sign up to request clarification or add additional context in comments.

Comments

1

Use json.json_normalize per rows and concat, last remove values before first . in columns names:

from pandas.io.json import json_normalize

c = ['photo.photo_replace', 'photo.photo_remove', 'text.text_remove']
df = pd.concat([json_normalize(x)[c] for x in df['editables']], ignore_index=True)
df.columns = df.columns.str.split('.').str[1]
print (df)
  photo_replace photo_remove text_remove
0          None         None        None
1          None         None        None

Comments

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.