简体   繁体   中英

Creating DataFrame from dictionary with different lengths of values

So, I'm looking to create a DataFrame from a dictionary similar to the follow:

d = {A: ['cat','dog','zebra'],
     B: ['frog,'lion'],
     C: ['snake','cat','ant','bird','turtle'],
     D: ['sloth']}

I want the dataframe to look as such:

Col1   Col2   Col3   Col4   Col5   Col6
 A    'cat'  'dog'  'zebra'  na     na
 B    'frog' 'lion'   na     na     na
 C    'snake' 'cat'  'ant'  'bird'  'turtle'
 D    'sloth'  na      na    na     na

Any ideas? thank you!

Use list comprehension for add keys of dictionary for nested lists, pass to DataFrame constructor and add DataFrame.add_prefix :

df = pd.DataFrame([[k,] + v for k, v in d.items()]).add_prefix('Col')
print (df)
  Col0   Col1  Col2   Col3  Col4    Col5
0    A    cat   dog  zebra  None    None
1    B   frog  lion   None  None    None
2    C  snake   cat    ant  bird  turtle
3    D  sloth  None   None  None    None

Or use DataFrame.from_dict with convert index to column and then set new columns names:

df = pd.DataFrame.from_dict(d, orient='index').reset_index()
df.columns = [f'col{x}' for x in range(1, len(df.columns) + 1)]
print (df)
  col1   col2  col3   col4  col5    col6
0    A    cat   dog  zebra  None    None
1    B   frog  lion   None  None    None
2    C  snake   cat    ant  bird  turtle
3    D  sloth  None   None  None    None      

If want starting by col1 is possible use rename with custom function:

f = lambda x: f'col{x+1}'
df = pd.DataFrame([[k,] + v for k, v in d.items()]).rename(columns=f)
print (df)
  col1   col2  col3   col4  col5    col6
0    A    cat   dog  zebra  None    None
1    B   frog  lion   None  None    None
2    C  snake   cat    ant  bird  turtle
3    D  sloth  None   None  None    None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM