0

I'm trying to get a dictionary with multiple list values and one string in to one dataframe.

Here's the information i'm trying to get in to the dataframe:

{'a': ['6449.70000', '1', '1.000'],
 'b': ['6446.40000', '1', '1.000'],
 'c': ['6449.80000', '0.04879000'],
 'h': ['6449.90000', '6449.90000'],
 'l': ['6362.00000', '6120.30000'],
 'o': '6442.30000',
 'p': ['6413.12619', '6353.50910'],
 't': [5272, 16027],
 'v': ['1299.86593468', '4658.87787321']}

The 3 values represented by key "a" all have their own names, say a1, a2 and a3 then b1, b2, and b3.. Preferably i want to define them myself. This goes for all information. So there should be 19 columns.

I've read a lot about this.

Based on these readings i think i could iterate trough it with a for loop, build separate dataframes and then join/merge them. But that seems more work then i think is required.

What is the most efficient / readable / logic way to do this using Python 3.6?

Michel K
  • 641
  • 1
  • 6
  • 18

1 Answers1

0

Do the cleaning up in pure Python:

colnames = []
values = []
for key,value in d.iteritems():
    if type(value) == list:
        for c in range(len(value)):
            colnames.append(key + str(c+1))
        values += value
    else:
        colnames.append(key + '1')
        values.append(value)

df = pd.DataFrame(values, index = colnames).T
Ken Wei
  • 3,020
  • 1
  • 10
  • 30