Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

python - 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!

question from:https://stackoverflow.com/questions/65829321/creating-dataframe-from-dictionary-with-different-lengths-of-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...