将对象列表字典转换为Pandas Dataframe

3duebb1j  于 2022-11-20  发布在  其他
关注(0)|答案(2)|浏览(196)

我有一个以下列组织格式存储的文件:

Dictionary
    List
        Object
            Attribute

具体如下:

dict = {
'0': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()]
'1': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()]
'2': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()]
'3': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()]
}

TestObject对象定义为:

import random

class TestObject:
    def __init__(self):
        self.id = random.randint()
        self.date = random.randint()
        self.size = random.randint()

本例中的属性并不重要,只是占位符。我所关心的是将此数据格式转换为 Dataframe 。具体来说,我希望将数据组织为类似以下格式:

|key|  object  |  id  | date | size |
|-- |  ------  | ---- | ---- | ---- |
| 0 |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
| 1 |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
| 2 |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
| 3 |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |
|   |TestObject| rand | rand | rand |

我发现了一个将列表字典转换为 Dataframe 的方法:

pandas.DataFrame.from_dict(dictionary)

但在本例中,我感兴趣的是从存储在列表中的对象中提取属性。

htzpubme

htzpubme1#

您可以使用列表解析:

pd.DataFrame([(k, o, o.id, o.date, o.size)
              for k, l in dic.items() for o in l],
             columns=['key', 'object', 'id', 'date', 'size']
            )

您首先需要在初始代码中修复一些问题:

import random

class TestObject:
    def __init__(self):
        self.id = random.randint(0,1)   # randint has 2 mandatory parameters
        self.date = random.randint(0,1) #
        self.size = random.randint(0,1) #

# better use "dic", "dict" is a python builtin
dic = {
'0': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()],
'1': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()],
'2': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()],
'3': [TestObject(),TestObject(),TestObject(),TestObject(),TestObject()]
}

输出示例:

key                                          object  id  date  size
0    0  <__main__.TestObject object at 0x7fc79371af10>   0     0     0
1    0  <__main__.TestObject object at 0x7fc79371aeb0>   1     0     1
2    0  <__main__.TestObject object at 0x7fc79371af70>   1     0     0
3    0  <__main__.TestObject object at 0x7fc79371c040>   1     0     1
4    0  <__main__.TestObject object at 0x7fc79371c0d0>   1     1     0
5    1  <__main__.TestObject object at 0x7fc79371c220>   1     1     1
6    1  <__main__.TestObject object at 0x7fc79371c1c0>   1     1     0
7    1  <__main__.TestObject object at 0x7fc79371c310>   0     1     0
8    1  <__main__.TestObject object at 0x7fc79371c400>   0     1     0
9    1  <__main__.TestObject object at 0x7fc79371c370>   0     0     1
10   2  <__main__.TestObject object at 0x7fc79371c4f0>   1     1     0
11   2  <__main__.TestObject object at 0x7fc79371c490>   0     0     1
12   2  <__main__.TestObject object at 0x7fc79371c5e0>   1     0     0
13   2  <__main__.TestObject object at 0x7fc79371c580>   1     0     1
14   2  <__main__.TestObject object at 0x7fc79371c640>   0     1     1
15   3  <__main__.TestObject object at 0x7fc79371c3d0>   0     1     1
16   3  <__main__.TestObject object at 0x7fc79371c730>   1     1     1
17   3  <__main__.TestObject object at 0x7fc79371c880>   1     0     1
18   3  <__main__.TestObject object at 0x7fc79371c850>   0     1     0
19   3  <__main__.TestObject object at 0x7fc79371c9a0>   0     1     1
mqkwyuun

mqkwyuun2#

在python中,每个对象都有一个__dict__属性,该属性列出了所有属性及其值:
第一个
使用dict解析,您可以轻松地实现您的目标,而不必指定所有需要的属性并添加带有类名的对象列:
第一次
然后用新的dict调用pd.DataFrame并转置它:
第一个

相关问题