Pandas:一种使用命名元组列表初始化数据框的简洁方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20012507/
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
Pandas: A clean way to initialize data frame with a list of namedtuple
提问by Mai
I'm new to pandas, therefore perhaps I'm asking a very stupid question. Normally initialization of data frame in pandas would be column-wise, where I put in dict with key of column names and values of list-like object with same length.
我是Pandas的新手,因此也许我问了一个非常愚蠢的问题。通常,pandas 中数据帧的初始化是按列进行的,我将列名的键和具有相同长度的类列表对象的值放入 dict 中。
But I would love to initialize row-wise without dynamically concat-ing rows. Say I have a list of namedtuple, is there a optimized operation that will give me a pandas data frame directly from it?
但是我很想在没有动态连接行的情况下逐行初始化。假设我有一个namedtuple 列表,是否有优化的操作可以直接从它给我一个pandas 数据框?
Many many thanks
非常感谢
采纳答案by filmor
The function you want is from_records.
您想要的功能是from_records。
For namedtupleinstances you must pass the _fieldsproperty of the namedtuple to the columnsparameter of from_records, in addition to a list of namedtuples:
例如,除了命名元组列表之外,namedtuple您还必须将_fieldsnamedtuple的属性传递给 的columns参数from_records:
df = pd.DataFrame.from_records(
[namedtuple_instance1, namedtuple_instance2],
columns=namedtuple_type._fields
)
If you have dictionaries, you can use it directly as
如果你有字典,你可以直接使用它作为
df = pd.DataFrame.from_records([dict(a=1, b=2), dict(a=2, b=3)])
回答by Andy Hayden
In a similar vein to creating a Series from a namedtuple, you can use the _fieldsattribute:
与从 namedtuple 创建 Series类似,您可以使用该_fields属性:
In [11]: Point = namedtuple('Point', ['x', 'y'])
In [12]: points = [Point(1, 2), Point(3, 4)]
In [13]: pd.DataFrame(points, columns=Point._fields)
Out[13]:
x y
0 1 2
1 3 4
Assuming they are all of the same type, in this example all Points.
假设它们都是相同的类型,在这个例子中都是Points。

