简体   繁体   English

将字典转换为具有键值对的 python 数据帧

[英]Convert dictionary to python dataframe which has key value pair

I have my dictionary as我有我的字典

{'id': '6576_926_1',
'name': 'xyz',
'm': 926,

0: {'id': '2896_926_2',
 'name': 'lmn',
 'm': 926},

1: {'id': '23_926_3',
 'name': 'abc',
 'm': 928}}

And I want to convert it into dataframe like我想把它转换成数据框

Id  Name    M

6576_926_1  Xyz 926

2896_926_2  Lmn 926

23_926_3    Abc 928

I am fine even if first row is not available as it doesn't have index.即使第一行不可用,我也很好,因为它没有索引。 There are around 1.3 MN records and so speed is very important.大约有 1.3 MN 记录,因此速度非常重要。 I tried using a for loop and append statement and it takes forever我尝试使用 for 循环和 append 语句,它需要永远

You can use a loop to convert each dictionary's entries into a list, and then use panda's .from_dict to convert to a dataframe.您可以使用循环将每个字典的条目转换为列表,然后使用 panda 的.from_dict转换为数据帧。 Here's the example given:这是给出的示例:

>>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
>>> pd.DataFrame.from_dict(data)
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

Use the following approach使用以下方法

import pandas as pd
data = pd.Dataframe(dict)
data = data.drop(0, axis=1)
data = data.drop(1, axis=1)

You can also try this你也可以试试这个

import pandas as pd

del dict['id']
del dict['name']
del dict['m']
pd.DataFrame(dict)

Try this code!!试试这个代码!! Still, complexity is O(n)仍然,复杂度是 O(n)

my_dict.pop('id')
my_dict.pop('name')
my_dict.pop('m')
data = [ row.values() for row in my_dict.values()]
pd.DataFrame(data=data, columns=['id','name','m'])

As you have mentioned that first row is not mandatory for you.正如您所提到的,第一行对您来说不是强制性的。 So, here i've tried this.所以,在这里我试过这个。 Hope this will solve your problem希望这能解决你的问题

import pandas as pd
lis = []
data = {
     0: {'id': '2896_926_2', 'name': 'lmn', 'm': 926},

     1: {'id': '23_926_3', 'name': 'abc', 'm': 928}
   }

for key,val in data.iteritems():  
    lis.append(val)
d = pd.DataFrame(lis)
print d   

Output --输出——

           id    m name
    0  2896_926_2  926  lmn
    1    23_926_3  928  abc

And if you want to id as your index then add set_index如果你想id作为你的索引然后添加set_index

for i,j in data.iteritems():  
    lis.append(j)
d = pd.DataFrame(lis)
d = d.set_index('id')
print d  

Output-输出-

              m name
id                  
2896_926_2  926  lmn
23_926_3    928  abc
mydict = {'id': '6576_926_1',
'name': 'xyz',
'm': 926,

0: {'id': '2896_926_2',
 'name': 'lmn',
 'm': 926},

1: {'id': '23_926_3',
 'name': 'abc',
 'm': 928}}

import pandas as pd
del mydict['id']
del mydict['name']
del mydict['m']
d = pd.DataFrame(mydict).T

import pandas as pd
data={'id': '6576_926_1','name': 'xyz','m': 926,0: {'id': '2896_926_2', 'name': 'lmn', 'm': 926},1: {'id': '23_926_3', 'name': 'abc','m': 928}}    
Id=[]
Name=[]
M=[]
for k,val in data.items():
    if type(val) is dict:
        Id.append(val['id'])
        Name.append(val['name'])
        M.append(val['m'])

df=pd.DataFrame({'Name':Name,'Id':Id,'M':M}) print(df) df=pd.DataFrame({'Name':Name,'Id':Id,'M':M}) 打印(df)

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

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