如何将列插入到 python Pandas 中的嵌套对象中?
我正在处理一个数据集,并且我有这个数据框,它只是数据集的一部分:
def make_decision(x):
decisions=pd.DataFrame(([{
"requests":" ",
"name":fake.name(),
# ****** NEED TO INSERT "IDS" HERE ****
"decision":[{
"ID":random.randint(0,400000),
# ....
}],
} for i in range(x)
]))
return decisions
decisions_data=make_decisions(100)
我需要对多个位置使用相同的 id。为此,我创建了另一个数据框:
def make_id(x):
ids=pd.DataFrame(([{
"id":random.randint(10000,1000000)
} for i in range(x)]))
return ids
ids_data=make_id(100)
在 make_decisions 内,我尝试过:
#decisions.decision
decisions["decision"].insert(loc=0,
column="ID",
value=ids.data)
这不起作用。有没有什么简单的方法可以在 pandas 的嵌套数据中插入列?解决方法是创建另一个数据框 decision
,在此处插入 ids,然后将 decision
插入到 decisions
中,如果我这样做就不好了您有一个深层嵌套的数据集。
I am working on a dataset and I have this data frame which is only a part of the dataset:
def make_decision(x):
decisions=pd.DataFrame(([{
"requests":" ",
"name":fake.name(),
# ****** NEED TO INSERT "IDS" HERE ****
"decision":[{
"ID":random.randint(0,400000),
# ....
}],
} for i in range(x)
]))
return decisions
decisions_data=make_decisions(100)
I need to use the same ids for multiple locations. For this I created another data frame:
def make_id(x):
ids=pd.DataFrame(([{
"id":random.randint(10000,1000000)
} for i in range(x)]))
return ids
ids_data=make_id(100)
inside make_decisions
, I tried:
#decisions.decision
decisions["decision"].insert(loc=0,
column="ID",
value=ids.data)
This did not work. Is there any easy way that I can insert a column inside nested data in pandas? A workaround is to create another data frame decision
, insert the ids here and then insert the decision
into the decisions
, which is not good if I you have a deep nested dataset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,给定以下玩具数据框:
您可以尝试这个(Python 3.9+):
So, given the following toy dataframes:
You could try this (Python 3.9+):