Reputation: 395
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
Upvotes: 0
Views: 40
Reputation: 24691
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]
Upvotes: 1
Reputation: 2318
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
Upvotes: 2
Reputation: 469
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
Upvotes: 2