Python 将实例列表放入Dataframe中,但一个属性是另一个字典列表
在过去的几个小时里,我一直在寻找过去的答案,但我仍然被卡住了。我有两门课:Python 将实例列表放入Dataframe中,但一个属性是另一个字典列表,python,Python,在过去的几个小时里,我一直在寻找过去的答案,但我仍然被卡住了。我有两门课: class Profile: def __init__(self, department_id, profile_id, items=None): self.department_id = department_id self.profile_id = profile_id self.items = items class Profile_Item: de
class Profile:
def __init__(self, department_id, profile_id, items=None):
self.department_id = department_id
self.profile_id = profile_id
self.items = items
class Profile_Item:
def __init__(self, profile_item, profile_value):
self.profile_item = profile_item
self.profile_value = profile_value
我试图将此列表放入一个数据框obj中,以便用于各种计算和数据透视。问题是MyItems属性是一个字典列表。我创建了一个函数,以形成可用于数据帧的字典:
def as_dict(self):
z = []
for x in self.items:
z.append({
'profile_item': x.profile_item,
'profile_value': x.profile_value
})
return {
'department_id': self.department_id,
'profile_id': self.profile_id,
'profile_items': z
}
如果我使用函数创建数据帧,这就是我停留在最后一列的地方:
df = pd.DataFrame([b.as_dict() for b in profile_list])
df.head()
+---+---------------+--------------+--------------------------------------------+
| | department_id | profile_id | profile_items |
+---+---------------+--------------+--------------------------------------------+
| 0 | 101500014 | 2.101250e+08 | [{'profile_item': 2.0, 'profile_value': 45 |
| 1 | 101500015 | 2.100991e+09 | [{'profile_item': 2.0, 'profile_value': 76 |
| 2 | 101500025 | 2.100760e+08 | [{'profile_item': 2.0, 'profile_value': 89 |
| 3 | 101500034 | 2.100000e+11 | [{'profile_item': 2.0, 'profile_value': 99 |
+---+---------------+--------------+--------------------------------------------+
它是相同键(profile_项和profile_值)的列表,但只是不同的值。基本上每个部门都有每个项目的价值。我希望数据框中的列具有部门id、配置文件id、配置文件项目和配置文件值。这篇文章:几乎让我达到了目的,但它只是根据一列字典列表重复了一列
df2 = pd.DataFrame(dict(
department_id=df.department_id.values.repeat(df['profile_items'].str.len()),
)).join(pd.DataFrame(df['profile_items'].sum()))
df2.head()
+---+---------------+--------------+---------------+
| | department_id | profile_item | profile_value |
+---+---------------+--------------+---------------+
| 1 | 101500014 | 9.0 | 120 |
| 2 | 101500014 | 10.0 | 39009 |
| 3 | 101500014 | 11.0 | 1403903 |
+---+---------------+--------------+---------------+
我有多只熊猫,我对熊猫不太熟悉。我想,在我通过函数或理解来访问pandas之前,我能以某种方式修复这些数据吗
我想将字典列表拆分为单独的行,然后将所述列连接到部门id和个人资料id:
+---------------+------------+--------------+---------------+
| department_id | profile_id | profile_item | profile_value |
+---------------+------------+--------------+---------------+
| 101500014 | 210125000 | 2 | 45 |
| 101500014 | 210125000 | 9 | 120 |
| 101500014 | 210125000 | 10 | 39009 |
| 101500014 | 210125000 | 11 | 1321312 |
| 101500014 | 210125000 | 15 | 12313213 |
| 101500015 | 2100991000 | 2 | 76 |
| 101500015 | 2100991000 | 9 | 4234 |
| 101500015 | 2100991000 | 10 | 34 |
| 101500015 | 2100991000 | 11 | 34 |
| 101500015 | 2100991000 | 15 | 3 |
| 101500015 | 2100991000 | 17 | 43 |
| 101500015 | 2100991000 | 18 | 4 |
| 101500015 | 2100991000 | 21 | 32 |
| 101500015 | 2100991000 | 22 | 23423 |
| 101500015 | 2100991000 | 35 | 2 |
+---------------+------------+--------------+---------------+
我根据您的数据创建了一个示例:
import pandas as pd
# create example
lst = [{'profile_item': 2.0, 'profile_value': 45}, {'profile_item': 2.0, 'profile_value': 76}, {'profile_item': 2.0, 'profile_value': 89}]
df = pd.DataFrame(data=[[lst]], columns=["lst"])
为回答您的问题,请输入以下代码:
profile_项由一个字典列表或仅包含一个字典的列表组成。如果在结尾处显示剪切,这不是一个理想的例子。这是一个字典列表。两个键:profile_项和profile_值,但每个部门有多个项和值。谢谢。这将拆分字典列表,但我还需要将其连接回原始数据帧。如果我没有正确地解释我的最终目标,我会更新我原来的帖子。你试过代码吗?这应该是我的错!我使用lambda函数在线路上创建了一个新的数据帧。绝对有效。
# code to create both columns
df = df.explode("lst")
df[['profile_item', 'profile_value']] = df.apply(lambda x: [x["lst"].get('profile_item'), x["lst"].get('profile_value')], axis=1, result_type="expand")