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
                It is the most commonly used pandas object. Creating pandas data-frame from lists using dictionary can be achieved in multiple ways. Let's discuss different ways to create a DataFrame one by one. With this method in Pandas, we can transform a dictionary of lists into a dataframe.
You can convert a dictionary to Pandas Dataframe using df = pd. DataFrame. from_dict(my_dict) statement.
pandas. DataFrame. from_dict() can be used to create a pandas DataFrame from Dict (Dictionary) object. This method takes parameters data , orient , dtype , columns and returns a DataFrame.
So we can use strings, numbers (int or float), or tuples as keys. Values can be of any type. We can also pass a dictionary to the dataframe function.
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
                        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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With