6

I have a dictionary of list which is like -

from collections import defaultdict
defaultdict(list,
            {'row1': ['Affinity'],
             'row2': ['Ahmc',
              'Garfield',
              'Medical Center'],
             'row3': ['Alamance','Macbeth'],
             'row4': [],
             'row5': ['Mayday']})

I want to convert this to a data frame. The output should look like-

ID  SYN1    SYN2    SYN3    SYN4    SYN5
row1    Affinity                
row2    Ahmc    Garfield    Medical Center      
row3    Alamance    Macbeth         
row4                    
row5    Mayday
2
  • Can you explain what "variable length of the values is varying" means? Commented Oct 15, 2018 at 14:36
  • hear 'row 2' contains 3 values..while 'row1' has just 1. Commented Oct 15, 2018 at 14:37

2 Answers 2

8

collections.defaultdict is a subclass of dict.

So you can just use pd.DataFrame.from_dict:

res = pd.DataFrame.from_dict(dd, orient='index')
res.columns = [f'SYN{i+1}' for i in res]

print(res)

          SYN1      SYN2            SYN3
row1  Affinity      None            None
row2      Ahmc  Garfield  Medical Center
row3  Alamance   Macbeth            None
row4      None      None            None
row5    Mayday      None            None
Sign up to request clarification or add additional context in comments.

Comments

4

Yes you can using Series

df=pd.Series(d).apply(pd.Series).fillna('')
Out[55]: 
             0         1               2
row1  Affinity                          
row2      Ahmc  Garfield  Medical Center
row3  Alamance   Macbeth                
row4                                    
row5    Mayday                          

Or from dataframe constructor

df=pd.DataFrame(data=list(d.values()),index=d.keys())
Out[64]: 
             0         1               2
row1  Affinity      None            None
row2      Ahmc  Garfield  Medical Center
row3  Alamance   Macbeth            None
row4      None      None            None
row5    Mayday      None            None

Then we create the column

df.columns='SYN'+(df.columns+1).astype(str)
df
Out[67]: 
          SYN1      SYN2            SYN3
row1  Affinity      None            None
row2      Ahmc  Garfield  Medical Center
row3  Alamance   Macbeth            None
row4      None      None            None
row5    Mayday      None            None

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.