Reputation: 1225
I have a dataframe which contains a column that has a list in it. The list can either be blank or have a dictionary at its first entry.
index labels
0 []
1 [{'id': 1178423440, 'node_id': 'MDU6TGFiZWwxMT...
2 [{'id': 1178425127, 'node_id': 'MDU6TGFiZWwxMT...
3 [{'id': 1213670757, 'node_id': 'MDU6TGFiZWwxMj...
4 [{'id': 1178430857, 'node_id': 'MDU6TGFiZWwxMT...
I want to assign the value with key = 'id' in place of the list (for each entry). Here is what I have done.
for i in issues['labels']:
if not i: continue
i=i[0]['id']
I realize this is assigning the value because the df remains the same (even though it runs). what am I doing wrong?
expected output:
index labels
0
1 1178423440
2 1178425127
3 1213670757
4 1178430857
edit:
say, if index 0 in the list within each row contained 2 or more dictionaries like so
[{'id': 1497192821, 'node_id': 'MDU6TGFiZWwxNDk3MTkyODIx', 'url': 'https://api.github.com/repos/chef/chef/labels/Focus:%20knife%20bootstrap', 'name': 'Focus: knife bootstrap', 'color': '92ef98', 'default': False, 'description': ''}, {'id': 1178425127, 'node_id': 'MDU6TGFiZWwxMTc4NDI1MTI3', 'url': 'https://api.github.com/repos/chef/chef/labels/Platform:%20Windows', 'name': 'Platform: Windows', 'color': 'a2c429', 'default': False, 'description': ''},
{'id': 1178435805, 'node_id': 'MDU6TGFiZWwxMTc4NDM1ODA1', 'url': 'https://api.github.com/repos/chef/chef/labels/Status:%20Waiting%20on%20Contributor', 'name': 'Status: Waiting on Contributor', 'color': '0052cc', 'default': False, 'description': 'A pull request that has unresolved requested actions from the author.'},
{'id': 525658991, 'node_id': 'MDU6TGFiZWw1MjU2NTg5OTE=', 'url': 'https://api.github.com/repos/chef/chef/labels/Type:%20Bug', 'name': 'Type: Bug', 'color': 'bfe5bf', 'default': False, 'description': "Doesn't work as expected."}]
how can I parse all the values of key='id' and append it to the labels
column at the same position?
expected op:
index labels
0 [] #has no entries
1 [1178423440,1178435805,525658991] # has 3 dictionaries with 3 different id values (values with key='id)
2 [1178425127,132131,13213] # slly, has 2 id values
3 [1389810] # has one id value
Upvotes: 1
Views: 42
Reputation: 863301
Use str
methods here for correct working if no match, then is returned NaN
:
issues['labels'] = issues['labels'].str[0].str.get('id')
If need integers with missing values use integer nan:
issues['labels'] = issues['labels'].str[0].str.get('id').astype('Int64')
EDIT: If each dictionary has id
use:
issues['labels'] = issues['labels'].apply(lambda x: [y['id'] for y in x])
If possible some dict has no id
add testing:
issues['labels'] = issues['labels'].apply(lambda x: [y['id'] for y in x if 'id' in y])
Upvotes: 1