0

I have a list of (user defined) objects known as "Instrument Measurements". These, have a title, accessible by Instrument_Measurement.name and a series of data accessible by Instrument_Measurement.data (both user defined methods).

These "Instrument Measurement" objects are items in a list called list_instr_objects. The number of items ("Instrument Measurements") changes/cannot be hard-coded.

I need to create a Dataframe with column titles Instrument_Measurement.name for each "Instrument Measurement" and data for that column of Instrument_Measurement.data for that respective "Instrument Measurement".

I'm trying to do this by creating a dictionary of these objects and then converting that to a Dataframe:

from collections import defaultdict
testdict = defaultdict(list)

for i in range(len(list_instr_objects)):
    testdict[list_instr_objects[i].name].append(list_instr_objects[i].data)

This doesn't work though, only the first data entry for each instrument is kept and the entries come out in a seemingly random order. I think this is because I'm passing a Series to the dictionary where I should be passing a list, but I can't think how else to do it/how to fix this.

Any thoughts?

Many thanks in advance.

2
  • 1
    The columns would definitely be in an arbitrary order (as dictionaries do not have orders): is that a problem? The other issue is that you don't want to append it to a list, you want to just set it as testdict[list_instr_objects[i].name] = list_instr_objects[i].data (and it doesn't have to be a defaultdict, just a dictionary) Commented Mar 25, 2014 at 19:21
  • Unfortunately after using just a dictionary and not appending to list, I'm still getting the same issue of having only the first element. Unfortunately I do require the order to be preserved so I'll look into a non-dictionary based method. Many thanks. Commented Mar 25, 2014 at 19:31

1 Answer 1

1

I think you can use OrderedDict to keep the order and use below code to generate the dataframe.

import pandas as pd
from collections import OrderedDict

testdict = OrderedDict()

for i in range(len(list_instr_objects)):
    testdict[list_instr_objects[i].name] = (list_instr_objects[i].data)

combined_data = pd.DataFrame(testdict)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.