<legend id='jUsA3'><style id='jUsA3'><dir id='jUsA3'><q id='jUsA3'></q></dir></style></legend>
    • <bdo id='jUsA3'></bdo><ul id='jUsA3'></ul>

    <small id='jUsA3'></small><noframes id='jUsA3'>

      <i id='jUsA3'><tr id='jUsA3'><dt id='jUsA3'><q id='jUsA3'><span id='jUsA3'><b id='jUsA3'><form id='jUsA3'><ins id='jUsA3'></ins><ul id='jUsA3'></ul><sub id='jUsA3'></sub></form><legend id='jUsA3'></legend><bdo id='jUsA3'><pre id='jUsA3'><center id='jUsA3'></center></pre></bdo></b><th id='jUsA3'></th></span></q></dt></tr></i><div id='jUsA3'><tfoot id='jUsA3'></tfoot><dl id='jUsA3'><fieldset id='jUsA3'></fieldset></dl></div>
    1. <tfoot id='jUsA3'></tfoot>

    2. 将数据框转换为元组列表字典

      Convert dataframe to dictionary of list of tuples(将数据框转换为元组列表字典)
      <legend id='A6pCD'><style id='A6pCD'><dir id='A6pCD'><q id='A6pCD'></q></dir></style></legend>
        <bdo id='A6pCD'></bdo><ul id='A6pCD'></ul>
      • <tfoot id='A6pCD'></tfoot>

                <i id='A6pCD'><tr id='A6pCD'><dt id='A6pCD'><q id='A6pCD'><span id='A6pCD'><b id='A6pCD'><form id='A6pCD'><ins id='A6pCD'></ins><ul id='A6pCD'></ul><sub id='A6pCD'></sub></form><legend id='A6pCD'></legend><bdo id='A6pCD'><pre id='A6pCD'><center id='A6pCD'></center></pre></bdo></b><th id='A6pCD'></th></span></q></dt></tr></i><div id='A6pCD'><tfoot id='A6pCD'></tfoot><dl id='A6pCD'><fieldset id='A6pCD'></fieldset></dl></div>
              1. <small id='A6pCD'></small><noframes id='A6pCD'>

                  <tbody id='A6pCD'></tbody>

                本文介绍了将数据框转换为元组列表字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个如下所示的数据框

                I have a dataframe that looks like the following

                    user                             item  
                0  b80344d063b5ccb3212f76538f3d9e43d87dca9e          The Cove - Jack Johnson   
                1  b80344d063b5ccb3212f76538f3d9e43d87dca9e  Entre Dos Aguas - Paco De Lucia   
                2  b80344d063b5ccb3212f76538f3d9e43d87dca9e            Stronger - Kanye West   
                3  b80344d063b5ccb3212f76538f3d9e43d87dca9e    Constellations - Jack Johnson   
                4  b80344d063b5ccb3212f76538f3d9e43d87dca9e      Learn To Fly - Foo Fighters   
                
                rating  
                0       1  
                1       2  
                2       1  
                3       1  
                4       1  
                

                并想实现如下结构:

                dict-> list of tuples
                user-> (item, rating)
                
                b80344d063b5ccb3212f76538f3d9e43d87dca9e -> list((The Cove - Jack 
                Johnson, 1), ... , )
                

                我能做到:

                item_set = dict((user, set(items)) for user, items in 
                data.groupby('user')['item'])
                

                但这只会让我半途而废.如何从 groupby 中获取相应的评分"值?

                But that only gets me halfways. How do I get the corresponding "rating" value from the groupby?

                推荐答案

                设置user为索引,使用df.apply转换成元组,使用分组索引df.groupby(level=0) 并使用 dfGroupBy.agg 获取列表并使用 df.to_dict 转换为字典:

                Set user as index, convert to tuple using df.apply, groupby index using df.groupby(level=0) and get a list using dfGroupBy.agg and convert to dictionary using df.to_dict:

                In [1417]: df
                Out[1417]: 
                                                       user                             item  
                0  b80344d063b5ccb3212f76538f3d9e43d87dca9e          The Cove - Jack Johnson   
                1  b80344d063b5ccb3212f76538f3d9e43d87dca9e  Entre Dos Aguas - Paco De Lucia   
                2  b80344d063b5ccb3212f76538f3d9e43d87dca9e            Stronger - Kanye West   
                3  b80344d063b5ccb3212f76538f3d9e43d87dca9e    Constellations - Jack Johnson   
                4  b80344d063b5ccb3212f76538f3d9e43d87dca9e      Learn To Fly - Foo Fighters   
                
                   rating  
                0       1  
                1       2  
                2       2  
                3       2  
                4       2  
                
                In [1418]: df.set_index('user').apply(tuple, 1)
                             .groupby(level=0).agg(lambda x: list(x.values))
                             .to_dict()
                Out[1418]: 
                {'b80344d063b5ccb3212f76538f3d9e43d87dca9e': [('The Cove - Jack Johnson', 1),
                  ('Entre Dos Aguas - Paco De Lucia', 2),
                  ('Stronger - Kanye West', 2),
                  ('Constellations - Jack Johnson', 2),
                  ('Learn To Fly - Foo Fighters', 2)]}
                

                这篇关于将数据框转换为元组列表字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                <small id='u4kB2'></small><noframes id='u4kB2'>

                <i id='u4kB2'><tr id='u4kB2'><dt id='u4kB2'><q id='u4kB2'><span id='u4kB2'><b id='u4kB2'><form id='u4kB2'><ins id='u4kB2'></ins><ul id='u4kB2'></ul><sub id='u4kB2'></sub></form><legend id='u4kB2'></legend><bdo id='u4kB2'><pre id='u4kB2'><center id='u4kB2'></center></pre></bdo></b><th id='u4kB2'></th></span></q></dt></tr></i><div id='u4kB2'><tfoot id='u4kB2'></tfoot><dl id='u4kB2'><fieldset id='u4kB2'></fieldset></dl></div>
                  <tbody id='u4kB2'></tbody>

                      <bdo id='u4kB2'></bdo><ul id='u4kB2'></ul>

                    • <tfoot id='u4kB2'></tfoot>
                        <legend id='u4kB2'><style id='u4kB2'><dir id='u4kB2'><q id='u4kB2'></q></dir></style></legend>