0

I have a list that I am trying to split into a pandas data frame. I can split simple lists by comma, but there are commas in the values that I need to split. Here is the way my list is setup:

[{'Name':'Smith, John','Date of Birth':datetime.datetime(2016, 5, 10, 0, 0),'Email Address':'[email protected]'},
 {'Name':'Smith, Jane','Date of Birth':datetime.datetime(2010, 6, 5, 0, 0),'Email Address':'[email protected]'},
 {'Name':'Doe, Monica','Date of Birth':datetime.datetime(2012, 10, 3, 0, 0),'Email Address':'[email protected]'}]

I have tried various ways to split the data, but it always comes out odd. The expected result should have been:

    Name           Date of Birth        Email Address
0   Smith, John    05-10-2016           [email protected]
1   Smith, Jane    06-05-2010           [email protected]
2   Doe, Monica    10-03-2012           [email protected]

but I keep getting things like below where the title is still in the column.

'Name':'Smith, John'    

Any help would be greatly appreciated

1
  • You should show your code, its not clear how you're getting things like that given you're passing in a list of dictionaries Commented Nov 19, 2019 at 15:17

3 Answers 3

2

Does passing the list into pandas.DataFrame() not work?

import pandas
import datetime

l = [{'Name':'Smith, John','Date of Birth':datetime.datetime(2016, 5, 10, 0, 0),'Email Address':'[email protected]'},
     {'Name':'Smith, Jane','Date of Birth':datetime.datetime(2010, 6, 5, 0, 0),'Email Address':'[email protected]'},
     {'Name':'Doe, Monica','Date of Birth':datetime.datetime(2012, 10, 3, 0, 0),'Email Address':'[email protected]'}]

df  = pandas.DataFrame(l)
print(df)

gives me:

  Date of Birth   Email Address         Name
0    2016-05-10   [email protected]  Smith, John
1    2010-06-05  [email protected]  Smith, Jane
2    2012-10-03     [email protected]  Doe, Monica
Sign up to request clarification or add additional context in comments.

1 Comment

It seems as if it is the simple solution that makes the most sense. I was trying to loop through the list and splitting them, and them appending them to a DataFrame. This is much easier. Thanks!
2
import pandas as pd
import datetime

data = [{'Name':'Smith, John','Date of Birth':datetime.datetime(2016, 5, 10, 0,      0),'Email Address':'[email protected]'},
{'Name':'Smith, Jane','Date of Birth':datetime.datetime(2010, 6, 5, 0, 0),'Email Address':'[email protected]'},
{'Name':'Doe, Monica','Date of Birth':datetime.datetime(2012, 10, 3, 0, 0),'Email Address':'[email protected]'}]

df = pd.DataFrame(data=data)

The output of df will be:

 Date of Birth   Email Address         Name
0    2016-05-10   [email protected]  Smith, John
1    2010-06-05  [email protected]  Smith, Jane
2    2012-10-03     [email protected]  Doe, Monica

Comments

1

You can actually just pass that into a pandas.DataFrame() call and it'll work more-or-less as you want (but with the indentation/justification changed up slightly):

>>> import pandas as pd
>>> my_list = [{'Name':'Smith, John','Date of Birth':datetime.datetime(2016, 5, 10, 0, 0),'Email Address':'[email protected]'},
...  {'Name':'Smith, Jane','Date of Birth':datetime.datetime(2010, 6, 5, 0, 0),'Email Address':'[email protected]'},
...  {'Name':'Doe, Monica','Date of Birth':datetime.datetime(2012, 10, 3, 0, 0),'Email Address':'[email protected]'}]
>>> x = pd.DataFrame(my_list)
>>> print(x)
          Name Date of Birth   Email Address
0  Smith, John    2016-05-10   [email protected]
1  Smith, Jane    2010-06-05  [email protected]
2  Doe, Monica    2012-10-03     [email protected]

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.