1

I have list of dict of dicts in the following form:

[{0:{'city':'newyork', 'name':'John', 'age':'30'}}, {0:{'city':'newyork', 'name':'John', 'age':'30'}},]

I want to create pandas DataFrame in the following form:

city name age newyork John 30 newyork John 30

Tried a lot but without any success

can you help me?

Okroshiashvili
  • 3,677
  • 2
  • 26
  • 40

5 Answers5

3

Use list comprehension with concat and DataFrame.from_dict:

L = [{0:{'city':'newyork', 'name':'John', 'age':'30'}},
 {0:{'city':'newyork', 'name':'John', 'age':'30'}}]

df = pd.concat([pd.DataFrame.from_dict(x, orient='index') for x in L])
print (df)
   name age     city
0  John  30  newyork
0  John  30  newyork

Solution with multiple keys with new column id should be:

L = [{0:{'city':'newyork', 'name':'John', 'age':'30'}, 
      1:{'city':'newyork1', 'name':'John1', 'age':'40'}},
     {0:{'city':'newyork', 'name':'John', 'age':'30'}}]

L1 = [dict(v, id=k) for x in L for k, v in x.items()]
print (L1)
[{'name': 'John', 'age': '30', 'city': 'newyork', 'id': 0}, 
 {'name': 'John1', 'age': '40', 'city': 'newyork1', 'id': 1}, 
 {'name': 'John', 'age': '30', 'city': 'newyork', 'id': 0}]
df = pd.DataFrame(L1)
print (df)
  age      city  id   name
0  30   newyork   0   John
1  40  newyork1   1  John1
2  30   newyork   0   John
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2
from pandas import DataFrame

ldata = [{0: {'city': 'newyork', 'name': 'John', 'age': '30'}},
         {0: {'city': 'newyork', 'name': 'John', 'age': '30'}}, ]

# 根据上面的ldata创建一个Dataframe
df = DataFrame(d[0] for d in ldata)
print(df)
"""
The answer is:
  age     city  name
0  30  newyork  John
1  30  newyork  John
"""
lihua
  • 21
  • 1
  • You might consider adding a few explanatory sentences as this increases the value of your answer for other users. – MBT May 15 '18 at 08:45
1
import pandas as pd 
d = [{0:{'city':'newyork', 'name':'John', 'age':'30'}},{0:{'city':'newyork', 'name':'John', 'age':'30'}},]   
df = pd.DataFrame([list(i.values())[0] for i in d])
print(df)

Output:

  age     city  name
0  30  newyork  John
1  30  newyork  John
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

You can use:

In [41]: df = pd.DataFrame(next(iter(e.values())) for e in l)

In [42]: df
Out[42]: 
  age     city  name
0  30  newyork  John
1  30  newyork  John
llllllllll
  • 16,169
  • 4
  • 31
  • 54
0

Came to new solution. Not as straightforward as posted here but works properly

L = [{0:{'city':'newyork', 'name':'John', 'age':'30'}},
     {0:{'city':'newyork', 'name':'John', 'age':'30'}}]


df = [L[i][0] for i in range(len(L))]


df = pd.DataFrame.from_records(df)
Okroshiashvili
  • 3,677
  • 2
  • 26
  • 40