简体   繁体   中英

create a dictionary from a dataframe in Python

For the following data, how can i create a dictionary with id as the key and value of array of tuple [(x, y)]

  id  x  y
0  a  1  2
1  a  2  3
2  b  3  4

The expected dictionary is

{a: [(1,2), (2,3)], b:[(3,4)]}

When iterating over the table (however you are doing it) check if the id is already in the dict and append to the existing value or add the new key:

data = {}
for id,x,y in SOURCE:
    if id in data:
        data[id].append((x,y))
    else:
        data[id] = [(x,y)]

The solution offered by @tadhg-mcdonald-jensen seems a perfect opportunity to take advantage of a defaultdict:

from collections import defaultdict

data = defaultdict(list)

with open(SOURCE_FILE_NAME) as source:
    for line in source:
        id, x, y = line.rstrip().split()
        data[id].append((x, y))

This avoids the decision to set or append -- simply append and let the defaultdict do the right thing for you.

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