0

I have a dictionary where the values are lists like the following:

d= {u'2012-06-08': [list_element_0, list_element_1, list_element_2],
 u'2012-06-09': [list_element_0, list_element_1, list_element_2],
 u'2012-06-10': [list_element_0, list_element_1, list_element_2]}

I'd like to create a dataframe for with 4 columns: [column_for_dict_keys, column_for_elements_in_list_at_index_0, column_for_elements_in_list_at_index_1, column_for_elements_in_list_at_index_2]

I found how to make a regular dictionary into a dataframe here, but I don't know how to modify it for my specific case

J.spenc
  • 339
  • 2
  • 3
  • 11
  • Please share the expected output based on the sample input you shared. – Mayank Porwal Oct 18 '20 at 02:55
  • 1
    From what I understood, try `pd.DataFrame(data = [[key, *value] for key, value in d.items()], columns = ['column_for_dict_keys', 'column_for_elements_in_list_at_index_0', 'column_for_elements_in_list_at_index_1', 'column_for_elements_in_list_at_index_2'])` which will create a DataFrame with four columns using your dict keys and values as data. – Felipe Whitaker Oct 18 '20 at 03:01

1 Answers1

1

Let's try:

pd.DataFrame(d).T.reset_index()

Output:

        index               0               1               2
0  2012-06-08  list_element_0  list_element_1  list_element_2
1  2012-06-09  list_element_0  list_element_1  list_element_2
2  2012-06-10  list_element_0  list_element_1  list_element_2
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74