Reputation: 885
I have a dictionary:
employer =
{'CrntEmp_city': ('XXX', 'XXX'),
'CrntEmp_cntry': ('XXX', 'XXX'),
'CrntEmp_orgNm': ('XXXX LLC', 'YYYY LLC'),
'CrntEmp_orgPK': ('1234567891', '1234567899'),
'CrntEmp_postlCd': ('12345', '12345'),
'CrntEmp_state': ('AK', 'AK'),
'CrntEmp_str1': ('999 XXX', '999 XXX'),
'CrntEmp_str2': ('XXXX', 'XXXX')}
and I want to read this into a DataFrame with one row and the eight columns corresponding to the keys. But however I try this, pandas will always give me two columns (by splitting the tuple where the comma is). Example:
pd.DataFrame([tuple(i) for i in employ.values()])
returns
0 1
0 XXXX LLC YYYY LLC
1 XXXX XXXX
2 999 XXX 999 XXX
3 XXX XXX
4 12345 12345
5 XXX XXX
6 AK AK
7 1234567891 1234567899
What I want is this:
0 1
0 (XXXX LLC, YYYY LLC) (XXXX, XXXX)
etc.
Hence this command fails:
pd.DataFrame([tuple(i) for i in employ.values()],columns=employ.keys(),index=[0])
Any ideas?
Upvotes: 2
Views: 1125
Reputation: 109546
You basically want something like: pd.DataFrame({'col1': [(a, b)], 'col2': [(c, d)]})
You can achieve that using a dictionary comprehension as follows in Python 2. For Python 3, use employer.iter()
instead.
>>> pd.DataFrame({k: [tuple(v)] for k, v in employer.iteritems()})
CrntEmp_city CrntEmp_cntry CrntEmp_orgNm CrntEmp_orgPK CrntEmp_postlCd CrntEmp_state CrntEmp_str1 CrntEmp_str2
0 (XXX, XXX) (XXX, XXX) (XXXX LLC, YYYY LLC) (1234567891, 1234567899) (12345, 12345) (AK, AK) (999 XXX, 999 XXX) (XXXX, XXXX)
Upvotes: 1
Reputation: 191
Try forcing the data to be one column by first defining it as a Series:
import pandas as pd
pd.DataFrame(data=pd.Series(employer.values()))
Then the output matches your example desired result (which does not match your original description, but I assume you can take it from here).
0
0 (XXXX LLC, YYYY LLC)
1 (XXXX, XXXX)
2 (999 XXX, 999 XXX)
3 (XXX, XXX)
4 (12345, 12345)
5 (XXX, XXX)
6 (AK, AK)
7 (1234567891, 1234567899)
Upvotes: 0
Reputation: 862641
I think you can use iteritems()
:
import pandas as pd
employer = {'CrntEmp_city': ('XXX', 'XXX'),
'CrntEmp_cntry': ('XXX', 'XXX'),
'CrntEmp_orgNm': ('XXXX LLC', 'YYYY LLC'),
'CrntEmp_orgPK': ('1234567891', '1234567899'),
'CrntEmp_postlCd': ('12345', '12345'),
'CrntEmp_state': ('AK', 'AK'),
'CrntEmp_str1': ('999 XXX', '999 XXX'),
'CrntEmp_str2': ('XXXX', 'XXXX')}
print pd.DataFrame([i for i in employer.iteritems()])
0 1
0 CrntEmp_orgNm (XXXX LLC, YYYY LLC)
1 CrntEmp_str2 (XXXX, XXXX)
2 CrntEmp_str1 (999 XXX, 999 XXX)
3 CrntEmp_cntry (XXX, XXX)
4 CrntEmp_postlCd (12345, 12345)
5 CrntEmp_city (XXX, XXX)
6 CrntEmp_state (AK, AK)
7 CrntEmp_orgPK (1234567891, 1234567899)
Upvotes: 1