简体   繁体   English

当键的值是列表列表时,如何将字典转换为 dataframe?

[英]How to convert dictionary to dataframe when values of keys are list of list?

I have dataframe that looks like this..我有 dataframe 看起来像这样..

{'abc2035': [['abc3007', 2],['abc2006', 2],['abc2003', 1],['abc3000', 3]],
 'abc2001': [['abc3002', 1],['abc2096', 2],['abc2003', 2],['abc3009', 3],['abc2015',3]],
 'abc2052': [['abc1003', 2],['abc2090', 2]],
.
.
.}

I want to convert it to a dataframe like this..我想将它转换为像这样的 dataframe ..

userID    with_common   no_of_common
abc2035    abc3007       2
abc2035    abc2006       2
abc2035    abc2003       1
abc2035    abc3000       3
abc2001    abc3002       1
abc2001    abc2096       2
abc2001    abc2003       2
abc2001    abc3009       3
abc2001    abc2015       3
.
.
.

Any help on this?对此有什么帮助吗?

Use:利用:

cols = ['userID',' with_common','no_of_common']
df = pd.DataFrame([(k, *x) for k, v in d.items() for x in v], columns=cols)
print (df)
     userID  with_common  no_of_common
0   abc2035      abc3007             2
1   abc2035      abc2006             2
2   abc2035      abc2003             1
3   abc2035      abc3000             3
4   abc2001      abc3002             1
5   abc2001      abc2096             2
6   abc2001      abc2003             2
7   abc2001      abc3009             3
8   abc2001      abc2015             3
9   abc2052      abc1003             2
10  abc2052      abc2090             2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM