17

I would like to populate a pandas dataframe from attributes of a list of classes generated via 'append'. (not sure this is the right term to describe 'allFoo' below Here is a stripped down example code:

class foo(object):
def __init__(self,input):
    self.val=input
    #in real life, there will be many more attributes in this class
allFoo=[];
for i in range(10):
    allFoo.append(foo(i))

now I would like to define a new pandas data frame 'df' that gets populated from allFoo.val (and not any other attributes)

something like this:

df[0]=foo[0].val
df[1]=foo[1].val

etc

I am coming from matlab where I would try something like this: dataFrame=allFoo[:].val

how can I achieve this in python/pandas?

2
  • Sorry are you just asking how to add a new column/array of values? Commented Dec 3, 2015 at 11:53
  • 1
    If so then dataFrame['val'] = allFoo should work Commented Dec 3, 2015 at 11:55

3 Answers 3

36

For your "stripped-down" example the following code would do the job:

pd.DataFrame([f.val for f in allFoo], columns=['val'])

In a slightly more general case, where you are sure you can take all field values from your objects, the following should work just as well:

pd.DataFrame([vars(f) for f in allFoo])

In a yet more general case, when your objects may contain some fields that you need in the data frame, and other fields which you do not, there is no way around specifying this list of fields. The following code might help then:

fields = ['val', 'other_field']
pd.DataFrame([{fn: getattr(f, fn) for fn in fields} for f in allFoo])

The moral: whenever you do not know a "built-in method" for something, list comprehension is your first choice.

Sign up to request clarification or add additional context in comments.

4 Comments

Is the code above placed within the class, within the function, or outside the class?
Wherever you need to create the data frame from a list of objects.
Thank you for the response :) I tried to input a version of option 3 into my code however got a error code saying that my object is not iterable. After a little research I believe that issue should be cleared by the [] surrounding the {fn..}, no? Or am I missing something else?
pd.DataFrame constructor expects a list. This is how it is done in "option 3". You should not need to add any more []-s, though.
0

Another option would be to use dict of a object

pd.DataFrame([f.__dict__ for f in allFoo])

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

As EdChum suggested in a comment:

import pandas as pd
allfoo = [1, 2, 3, 4]
df = pd.DataFrame()
df["val"] = allfoo
print df

Outputs

   val
0    1
1    2
2    3
3    4

1 Comment

for some reason, the command 'df = pd.DataFrame()' crashes my spyder python 2.7 on windows 7. So I went with KT's answer below which does what I need.

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.