I have dataclass containing other dataclass as its field:
@dataclass
class Bar:
abc: int
bed: int
asd: int
@dataclass
class Foo:
xy: int
yz: Bar
then I try to serialize it to csv by pandas like this:
dataset = [Foo(xy=1, yz=Bar(abc=1, bed=2, asd=3))]
pd_dataset = pandas.DataFrame(vars(row) for row in dataset)
pd_dataset.to_csv('dataset_example.csv', index=False)
but the result I get is kinda different than I want to achieve. To be precise I now get:
xy,yz
1,"Bar(abc=1, bed=2, asd=3)"
and I want:
xy,yz_abc,yz_bed,yz_asd
1,1,2,3
Can you help me getting it right? I tried to write my own serialization function and do something like:
pandas.DataFrame(asdict(row, dict_factory=row_to_dict) for row in dataset)
but I can't get how to correctly write it.