Reputation: 623
Assigne(column name)
[{'id': 2342343, 'username': 'Raj', 'color': '#08c7e0', 'profilePicture': None}]
[{'id': 764665, 'username': 'mac', 'color': '#08c7e0', 'profilePicture': None}]
name = [d.get('username') for d in df.assigne]
print (name)
AttributeError: 'list' object has no attribute 'get'
Any suggestions would be helpful, Thanks.
Upvotes: 1
Views: 3327
Reputation: 13255
You are accessing list from loop, to access dictionary use:
name = [d[0].get('username') for d in df.assigne]
print (name)
['Raj', 'mac']
EDIT 1: If the row contains empty list use:
name = [d[0].get('username') for d in df.assigne if d]
EDIT 2: If multiple dictionaries in list:
df = pd.DataFrame([[[{'id':2342343, 'username':'Raj', 'color':'#08c7e0', 'profilePicture':None}]],
[[{'id':764665, 'username':'mac', 'color':'#08c7e0', 'profilePicture':None}]],
[[{'id':2342343, 'username':'Raj', 'color':'#08c7e0', 'profilePicture':None},
{'id':2342343, 'username':'mac', 'color':'#08c7e0', 'profilePicture':None}]],
[[]],
[[{'id':2342343, 'username':'sand', 'color':'#08c7e0', 'profilePicture':None}]]],
columns=['Assigne'])
name = [x.get('username') for d in df.Assigne if d for x in d]
print(name)
['Raj', 'mac', 'Raj', 'mac', 'sand']
Upvotes: 2