1

I have list of list(t):

[[{'CreationDate': b"D:20191125142104+05'00'",
   'Creator': b'PDF-XChange Editor 7.0.325.1',
   'ModDate': b"D:20191125142754+05'00'",
   'Producer': b'PDF-XChange Core API SDK (7.0.325.1)'}],
 [{'CreationDate': b"D:20200215153643+05'00'",
   'Creator': b'Adobe Acrobat 11.0.23',
   'ModDate': b"D:20200215191411+05'00'",
   'Producer': b'Adobe Acrobat Pro 11.0.23 Paper Capture Plug-in'}],
 [{'CreationDate': b"D:20200215153532+05'00'",
   'Creator': b'Adobe Acrobat 11.0.23',
   'ModDate': b"D:20200215191426+05'00'",
   'Producer': b'Adobe Acrobat Pro 11.0.23 Paper Capture Plug-in'}]]

I need create a DataFrame, where columns=['CreationDate', 'Creator', 'ModDate', 'Producer']. I try to do next: pd.DataFrame(t, columns=['CreationDate', 'Creator', 'ModDate', 'Producer']) and I get error:

 4 columns passed, passed data had 1 columns

If I do pd.DataFrame(t[0], columns=['CreationDate', 'Creator', 'ModDate', 'Producer']), I get an one-row DataFrame. How to do a good DataFrame? Thanks.

2 Answers 2

1

You can select first lists from nested lists in list comprehension:

df = pd.DataFrame([x[0] for x in t])

Or flatten nested lists, then get all nested lists:

df = pd.DataFrame([y for x in t for y in x])

print (df)
                 CreationDate                          Creator  \
0  b"D:20191125142104+05'00'"  b'PDF-XChange Editor 7.0.325.1'   
1  b"D:20200215153643+05'00'"         b'Adobe Acrobat 11.0.23'   
2  b"D:20200215153532+05'00'"         b'Adobe Acrobat 11.0.23'   

                      ModDate  \
0  b"D:20191125142754+05'00'"   
1  b"D:20200215191411+05'00'"   
2  b"D:20200215191426+05'00'"   

                                            Producer  
0            b'PDF-XChange Core API SDK (7.0.325.1)'  
1  b'Adobe Acrobat Pro 11.0.23 Paper Capture Plug...  
2  b'Adobe Acrobat Pro 11.0.23 Paper Capture Plug...  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It's really helps me!
1

Use concat and from_dict:

df=pd.concat([pd.DataFrame().from_dict(x) for x in ls])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.