numpy 通过比较python中每个列表值的第一个索引,将多键字典转换为panda

irlmq6kh  于 2022-11-24  发布在  Python
关注(0)|答案(1)|浏览(124)

我有一本字典,格式如下:

{
    "Description" : [[".1","test"],[".3","test1"],[".4","test2"]],
    "Description1": [[".1","196"],[".4","197"],[".3","198"]],
    "Description3": [[".1","2"],[".3","2"]],
    "Description3": [[".1",".1.3"],[".3",".1.4"],[".4",".1.5"]]
}

其中每个键都有一个二维数组,在我的用例中,list的第一个值是一个搜索/索引。我必须从列表中取出第一个元素(.1或.3或.4),并过滤相同字典中其他键的值,使其成为如下所示的行格式。
例外输出为:

0 : ["test","196","2",".1.3"]
1 : ["test1","198","2","1.4"]

注:如果索引在其他键中不存在,可以将其填充为“无”。
有没有什么方法可以更好地处理Pandas或麻木?

yzuktlbb

yzuktlbb1#

通过嵌套列表创建字典并传递给DataFrame构造函数:

d = {
"Description" : [[".1","test"],[".3","test1"],[".4","test2"]],
"Description1": [[".1","196"],[".4","197"],[".3","198"]],
"Description2": [[".1","2"],[".3","2"]],
"Description3": [[".1",".1.3"],[".3",".1.4"],[".4",".1.5"]]
}

df = pd.DataFrame({k: dict(v) for k, v in d.items()})
print (df)
   Description Description1 Description2 Description3
.1        test          196            2         .1.3
.3       test1          198            2         .1.4
.4       test2          197          NaN         .1.5

或者:

df1 = pd.DataFrame.from_dict({k: dict(v) for k, v in d.items()}, orient='index')
print (df1)
                .1     .3     .4
Description   test  test1  test2
Description1   196    198    197
Description2     2      2    NaN
Description3  .1.3   .1.4   .1.5

如果需要新词典:

print (df.T.to_dict('list'))
{'.1': ['test', '196', '2', '.1.3'], 
 '.3': ['test1', '198', '2', '.1.4'], 
 '.4': ['test2', '197', nan, '.1.5']}

相关问题