I have a dataframe like below:
df = pd.DataFrame({'id' : [1,2,3],
'attributes' : [{'dd' : True, 'budget' : '35k'}, {'dd' : True, 'budget' : '25k'}, {'dd' : True, 'budget' : '40k'}],
'prod.attributes' : [{'img' : 'img1.url', 'name' : 'millennials'}, {'img' : 'img2.url', 'name' : 'single'}, {'img' : 'img3.url', 'name' : 'married'}]})
df
id attributes prod.attributes
0 1 {'dd': True, 'budget': '35k'} {'img': 'img1.url', 'name': 'millennials'}
1 2 {'dd': True, 'budget': '25k'} {'img': 'img2.url', 'name': 'single'}
2 3 {'dd': True, 'budget': '40k'} {'img': 'img3.url', 'name': 'married'}
I have multiple such columns wherein I need to append all columns that have attributes
as suffix with the actual attributes
column as below:
op = pd.DataFrame({'id' : [1,2,3],
'attributes' : [{'dd' : True, 'budget' : '35k', 'prod' : {'img' : 'img1.url', 'name' : 'millennials'}}, \
{'dd' : True, 'budget' : '25k', 'prod' : {'img' : 'img2.url', 'name' : 'single'}},
{'dd' : True, 'budget' : '40', 'prod' : {'img' : 'img3.url', 'name' : 'married'}}]})
op
id attributes
0 1 {'dd': True, 'budget': '35k', 'prod': {'img': 'img1.url', 'name': 'millennials'}}
1 2 {'dd': True, 'budget': '25k', 'prod': {'img': 'img2.url', 'name': 'single'}}
2 3 {'dd': True, 'budget': '40', 'prod': {'img': 'img3.url', 'name': 'married'}}
I tried:
df['attributes'].apply(lambda x : x.update({'audience' : df['prod.attributes']}))
But I am getting all None
. Could someone please help me on this.