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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…