我们可以用dataclass
es这样做:
from dataclasses import dataclass
import pandas as pd
@dataclass
class MyDataClass:
i: int
s: str
df = pd.DataFrame([MyDataClass("a", 1), MyDataClass("b", 2)])
这使得DataFrame
df
如人们所期望的那样具有列i
和s
。
对于attrs
类,有没有简单的方法可以做到这一点?
我可以通过迭代对象的属性,构造一个类似dict[str, list]
(在本例中为{"i": [1, 2], "s": ["a", "b"]}
)类型的对象,并从中构造DataFrame来实现这一点,但直接支持attrs
对象会更好。
1条答案
按热度按时间gfttwv5a1#
You can access the dictionary at the heart of a dataclass like so
this outputs:
Knowing this, if you have an iterable
arr
of typeMyDataClass
, you can access the__dict__
attribute and construct a dataframedf outputs:
The limitation with this approach that if the
slots
option is used, then this will not work.Alternatively, it is possible to convert the data from a dataclass to a tuple or dictionary using
dataclasses.astuple
anddataclasses.asdict
respectively.The data frame can be also constructed using either of the following: