Python:制作包含两列项目的字典

时间:2018-03-06 22:24:56

标签: python list pandas dictionary group-by

我有这样的数据集:

user_id           time_location
  13            (2018-02-02, 190)
  12            (2018-06-02, 194)
  13            (2018-06-02, 194)
  16            (2018-02-02, 190)
  17            (2018-02-02, 190)
  11            (2018-05-02, 198)
  19            (2018-02-02, 190)
  15            (2018-05-02, 198)
  15            (2018-06-02, 194)

我想要的是一个词典列表,键是" time_location"列和值是每个键的user_id。这是一个示例输出:

List=[{(2018-02-02, 190): 13, 16,17,19},{(2018-06-02, 194): 12,13,15},{(2018-05-02, 198): 11,15}

有人可以帮我吗?

4 个答案:

答案 0 :(得分:3)

一种方法是使用collections.defaultdict

from collections import defaultdict

df = pd.DataFrame({'user_id': [13, 12, 13, 16, 17, 11, 19, 15, 15],
                   'time_location': [('2018-02-02', 190), ('2018-06-02', 194),
                                     ('2018-06-02', 194), ('2018-02-02', 190),
                                     ('2018-02-02', 190), ('2018-05-02', 198),
                                     ('2018-02-02', 190), ('2018-05-02', 198),
                                     ('2018-06-02', 194)]})

d = defaultdict(list)

for idx, row in df.iterrows():
    d[row['time_location']].append(row['user_id'])

d = [{k: v} for k, v in d.items()]

# [{('2018-02-02', 190): [13, 16, 17, 19]},
#  {('2018-06-02', 194): [12, 13, 15]},
#  {('2018-05-02', 198): [11, 15]}]

答案 1 :(得分:2)

一种方法是使用df.groupby()

df = pd.DataFrame({'user_id': [13, 12, 13, 16, 17, 11, 19, 15, 15],
                   'time_location': [('2018-02-02', 190), ('2018-06-02', 194),
                                     ('2018-06-02', 194), ('2018-02-02', 190),
                                     ('2018-02-02', 190), ('2018-05-02', 198),
                                     ('2018-02-02', 190), ('2018-05-02', 198),
                                     ('2018-06-02', 194)]})

d = df.groupby('time_location')['user_id'].apply(list).to_dict()
d = [{k: v} for k, v in d.items()]

# [{('2018-02-02', 190): [13, 16, 17, 19]},
#  {('2018-05-02', 198): [11, 15]},
#  {('2018-06-02', 194): [12, 13, 15]}]

答案 2 :(得分:1)

试试这个:

listOfDict = []
for pair in your_dataset: ## I'm assuming your data is a list of lists/tuples
    listOfDict.append({pair[1]: pair[0]})

答案 3 :(得分:1)

您也可以使用group_by

执行此操作
df.groupby("time_location")["user_id"].apply(list).to_dict()