Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create dataframe from dictionary of list with variable length

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
like image 549
anarchy Avatar asked Oct 15 '18 14:10

anarchy


People also ask

Can we create DataFrame from dictionary of lists?

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.

How do you turn a dictionary into a data frame?

You can convert a dictionary to Pandas Dataframe using df = pd. DataFrame. from_dict(my_dict) statement.

What method creates a pandas DataFrame from a dictionary?

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.

Can we create DataFrame using dictionary of tuples?

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.


2 Answers

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
like image 53
jpp Avatar answered Oct 16 '22 15:10

jpp


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
like image 23
BENY Avatar answered Oct 16 '22 16:10

BENY