嵌套列表到Pandas DataFrame的字典 [英] Dictionary of nested lists to pandas DataFrame

查看:101
本文介绍了嵌套列表到Pandas DataFrame的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习熊猫的工作原理,但我想我缺少明显的东西.

I'm trying to learn how pandas works but I assume I'm missing something obvious.

我有一个看起来像这样的文件:

I have a file looking like this :

dict_spl ={'doc1':[[('word11',1,1),('word12',1,2)]], 'doc2':[[('word21',2,1),('word22',2,2)]]}

我正在尝试获得一个如下所示的pandas DataFrame:

And I'm trying to obtain a pandas DataFrame looking like this:

# doc1 word11 1 1
# doc1 word12 1 2
# doc2 word21 2 1
# doc2 word22 2 2

我还没有找到一种在复制通用值的同时创建新列和新行的方法.

I haven't found a way to create both new columns and new rows while duplicating the common values.

推荐答案

您可以使用:

a = [[(k, *y) for y in v[0]] for k,v in dict_spl.items()]
a = [item for sublist in a for item in sublist]

df = pd.DataFrame(a, columns=list('abcd'))
print (df)
      a       b  c  d
0  doc1  word11  1  1
1  doc1  word12  1  2
2  doc2  word21  2  1
3  doc2  word22  2  2

我觉得有更好的解决方案,所以我问此处:

I feel there is better solution, so I asked here:

#Martijn Pieters♦'s solution
a = [(k, *t) for k, v in dict_spl.items() for t in v[0]]
df = pd.DataFrame(a, columns=list('abcd'))
print (df)
      a       b  c  d
0  doc2  word21  2  1
1  doc2  word22  2  2
2  doc1  word11  1  1
3  doc1  word12  1  2

这篇关于嵌套列表到Pandas DataFrame的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆