Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将pandas GroupBy中的多个列值聚合为dict_Python_Pandas_Dictionary_Group By_Aggregate - Fatal编程技术网

Python 将pandas GroupBy中的多个列值聚合为dict

Python 将pandas GroupBy中的多个列值聚合为dict,python,pandas,dictionary,group-by,aggregate,Python,Pandas,Dictionary,Group By,Aggregate,我有一个与这个问题相关的问题: 我的输入数据包含以下列: 例如,输入将具有以下格式 language, product_id, shelf_id, rank, fiction, pages English, 742005, 4560, 10.2, 1.0, 456 English, 6000075389352, 4560, 49, 1.0, 234 French, 899883993, 4560, 32, 0.0, 125 French, 731317391, 7868, 81, 1.0, 5

我有一个与这个问题相关的问题:

我的输入数据包含以下列:

例如,输入将具有以下格式

language, product_id, shelf_id, rank, fiction, pages
English, 742005, 4560, 10.2, 1.0, 456 
English, 6000075389352, 4560, 49, 1.0, 234
French, 899883993, 4560, 32, 0.0, 125
French, 731317391, 7868, 81, 1.0, 576
French, 235678655, 7868, 12, 1.0, 235
我想在language&shelf_id列上执行“groupby”,并获取每个产品_id的剩余属性列表。预期输出应具有以下格式:

Language,shelf\u id,{product\u id:[排名、小说、页面]}
用于每个分组记录

对于给定的输入,我的期望输出将如下所示:

language, shelf_id, mapping
English, 4560, {742005: [10.2, 1.0, 456], 6000075389352: [49, 1.0, 234]}
French, 4560, {899883993: [32, 0.0, 125]}
French, 7868, {731317391: [81, 1.0, 576], 235678655: [12, 1.0, 235]}
上述文章中提供的解决方案很好地解决了问题,如果只需要考虑一列(在生成的字典中):

这将产生:

      Lang  shelf_id                              mapping
0  English      4560  {742005: 10.2, 6000075389352: 49.0}
1   French      4560                    {899883993: 32.0}
2   French      7868   {731317391: 81.0, 235678655: 12.0}

有人能帮我把这个解决方案适应我的情况吗?任何建议都将不胜感激。

想法是创建一个新的系列
s
,其中包含
元组
项,元组中的第一项是
产品id
,第二项是包含列
等级
小说
页面
中相应值的列表,接下来,我们使用将系列
s
分组到
language
shelf\u id
上,并将数据聚合为字典:

df = pd.read_csv('file.csv', header=None)  
df.columns = ['Lang', 'product_id', 'shelf_id', 'rank_id']
(df.groupby(['Lang', 'shelf_id'], as_index=False)
   .apply(lambda x: dict(zip(x['product_id'], x['rank_id'])))
   .reset_index(name='mapping'))
s = pd.Series([(k, v) for k, *v in zip(df['product_id'],
                                       df['rank'], df['fiction'], df['pages'])])
                                       
df = s.groupby([df['language'], df['shelf_id']]).agg(
               lambda d: dict(d.tolist())).reset_index(name='mapping')
详细信息:

#print(s)
0           (742005, [10.2, 1.0, 456]) # --> product_id: [rank, fiction, pages]
1    (6000075389352, [49.0, 1.0, 234])
2        (899883993, [32.0, 0.0, 125])
3        (731317391, [81.0, 1.0, 576])
4        (235678655, [12.0, 1.0, 235])
dtype: object

# print(df)
  language  shelf_id                                                      mapping
0  English      4560  {742005: [10.2, 1.0, 456], 6000075389352: [49.0, 1.0, 234]}
1   French      4560                                {899883993: [32.0, 0.0, 125]}
2   French      7868   {731317391: [81.0, 1.0, 576], 235678655: [12.0, 1.0, 235]}

谢谢这正是我需要的。我也很喜欢你的解释。您的解决方案非常简单、清晰,非常符合Python。