简体   繁体   中英

How to create a list of lists using unique values in a dataframe column?

I have a dataframe as below where one ticket has multiple item associated with it.

| ticket_no | items |
|-----------|-------|
| 1         | Item1 |
| 1         | Item2 |
| 2         | Item3 |
| 2         | Item4 |
| 3         | Item5 |
| 3         | Item6 |
| 3         | Item7 |
| 3         | Item8 |

Need output as below.

[[Item1, Item2],[Item3, Item4], [Item5, Item6, Item7, Item8]]

I have tried below code. It works, but it is terribly slow.

data = pd.read_csv('data.csv')
item_list = []
for ticket_no in data['ticket_no'].unique():
    temp_data = list(data[data['ticket_no'] == ticket_no]['items'])
    if len(temp_data) == 1:
        pass
    else:
        item_list.append(temp_data)

Is there a faster way of doing this?

Use DataFrame.groupby with list to Series and then convert it to list s - output is nested lists:

item_list = data.groupby('ticket_no')['items'].apply(list).tolist()
print (item_list)
[['Item1', 'Item2'], ['Item3', 'Item4'], ['Item5', 'Item6', 'Item7', 'Item8']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM