将数据帧转换为元组列表的字典

时间:2017-08-25 13:49:29

标签: python pandas dictionary dataframe tuples

我的数据框如下所示

    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获得相应的“评级”值?

1 个答案:

答案 0 :(得分:4)

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script> <script type="text/javascript"> $("[id*=chkHeader]").live("click", function () { var chkHeader = $(this); var grid = $(this).closest("table"); $("input[type=checkbox]", grid).each(function () { if (chkHeader.is(":checked")) { $(this).attr("checked", "checked"); $("td", $(this).closest("tr")).addClass("selected"); } else { $(this).removeAttr("checked"); $("td", $(this).closest("tr")).removeClass("selected"); } }); }); $("[id*=chkChild]").live("click", function () { var grid = $(this).closest("table"); var chkHeader = $("[id*=chkHeader]", grid); if (!$(this).is(":checked")) { $("td", $(this).closest("tr")).removeClass("selected"); chkHeader.removeAttr("checked"); } else { $("td", $(this).closest("tr")).addClass("selected"); if ($("[id*=chkChild]", grid).length == $("[id*=chkChild]:checked", grid).length) { chkHeader.attr("checked", "checked"); } } }); 设置为索引,使用user转换为元组,使用df.apply转换为groupby索引并使用df.groupby(level=0)获取列表并使用dfGroupBy.agg转换为字典:

df.to_dict