1

I have a dictionary that has 3000 keys and each key has a list as its value,

example:

dictionary = {'yup': ['chocolate', 'pizza', 'rainbows'], 'tape': ['what', 'milk'], ....}

I want to convert this dictionary into a database but since I have unequal values I am unable to use the pandas command "pd.DataFrame(dictionary)". I received the error message "arrays must all be same length".

My question is, how do I still create a data frame (transpose it) and have the empty cells where there isn't data.

Example:

index   column1      column2   column3 

`'yup'  'chocolate'  'pizza'    'rainbows`'

'tape'  'what'       'milk'     blank
Maria Nazari
  • 660
  • 1
  • 9
  • 27

2 Answers2

5

Use values for values and keys for index

df = pd.DataFrame(list(dictionary.values()), index=dictionary.keys())

        0           1       2
yup     chocolate   pizza   rainbows
tape    what        milk    None

If need the column named index

df.reset_index()

    index   0           1       2
0   yup     chocolate   pizza   rainbows
1   tape    what        milk    None
rafaelc
  • 57,686
  • 15
  • 58
  • 82
2

Transform your values to pd.Series:

df = pd.DataFrame({k:pd.Series(v) for k,v in dictionary.items()}).T

              0      1         2
tape       what   milk       NaN
yup   chocolate  pizza  rainbows

Or:

df = pd.DataFrame.from_dict({k:pd.Series(v) for k,v in dictionary.items()},orient='index')

              0      1         2
tape       what   milk       NaN
yup   chocolate  pizza  rainbows
sacuL
  • 49,704
  • 8
  • 81
  • 106