chizze chizze - 7 months ago 24
Python Question

Pandas DataFrame column from a tuple

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?

Answer

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)