python 类属性到 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34065361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python class attributes to pandas dataframe
提问by jlarsch
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:
我想从通过“附加”生成的类列表的属性填充Pandas数据框。(不确定这是描述下面“allFoo”的正确术语这是一个精简的示例代码:
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)
现在我想定义一个从 allFoo.val 填充的新 Pandas 数据框 'df'(而不是任何其他属性)
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
我来自 matlab,在那里我会尝试这样的事情:dataFrame=allFoo[:].val
how can I achieve this in python/pandas?
我怎样才能在 python/pandas 中实现这一点?
回答by KT.
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.
寓意:当您不知道某事的“内置方法”时,列表理解是您的首选。
回答by Plasma
As EdChum suggested in a comment:
正如 EdChum 在评论中建议的那样:
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