使用pandas从数据框中的字典列表中提取值

发布于 2025-01-24 09:22:21 字数 1834 浏览 0 评论 0原文

我有这个带有4列的数据框。我想在一个单独的列中提取ResourceName(IE ID)。我尝试了各种方法和循环,但无法将其分开。

数据集:

用户名事件名称资源
xyz-dev_env_post_function[“ I-05FBB7A”}
stopInstances{“ resourceType”: “aws :: ec2 :: ec2 ::实例”,“ resourcename” : :ec2 ::实例“,“ resourceName”:“ I-08BD2475”},{“ resourceType”:“ aws :: ec2 :: ec2 ::实例”,“ resourcename”:“ i-0fd69dc1”} AWS :: ec2 ::实例“,“ resourceName”:“ I-0174DD38AEA”}]

我想要一个将具有来自Resource列的ID的列ID,LL看起来像这样:

用户名事件名称xyz-
dev_env_env_env_env_env_post_function_functionstopinstances[{{{{ {{{{{{{{{{{{{{{ “ ResourceType”:“ AWS :: ec2 ::实例”,“ ResourceName”:“ I-05FBB7A”}]I-05FBB7A“
XYZ-DEV_ENV_ENV_ENV_POST_FUNCTICT_FUNCTICTANCES[{“ resourceType)[{“ resourcetype” ResourceName“:” I-08BD2475“},{“ resourceType”:“ aws :: ec2 :: Instance”,“ resourcename”:“ i-0fd69dc1”},{“ resourceType” ,“ ResourceName”:“ I-0174DD38AEA”}]I-08BD2475,I-0FD69DC1,I-0174

这是Data.head(2).TO_DICT():

{'date':':':':':': {0:'28 -02-2022',1:'28 -02-2022'}, “事件名称”: {0:'stopinstances',1:'startInstances'}, '资源': { 0:'[{“ resourceType”:“ aws :: ec2 :: instance”,“ resourcename”:“ i-05fbb7a”}], 1:'[{“ resourceType”:“ aws :: ec2 :: instance”,“ resourceName”:“ i-08BD2475”},{“ resourceType”:“ aws :: ec2 :: ec2 ::实例”,“ resourceName”,“ resourceName”:“:” i-0fd69dc1“},{“ resourceType”:“ aws :: ec2 ::实例”,“ resourcename”:“ I-0174DD38AEA”}]'}, '用户名':{0:'xyz-dev_env_post_function',1: 'xyz-dev_env_post_function'}}

谢谢,

I have this dataframe with 4 columns. I want to extract resourceName (i.e IDs ) in one separate column. I tried various methods and loops but unable to seperate it.

Dataset:

UsernameEvent nameResources
XYZ-DEV_ENV_POST_functionStopInstances[{"resourceType":"AWS::EC2::Instance","resourceName":"i-05fbb7a"}]
XYZ-DEV_ENV_POST_functionStartInstances[{"resourceType":"AWS::EC2::Instance","resourceName":"i-08bd2475"},{"resourceType":"AWS::EC2::Instance","resourceName":"i-0fd69dc1"},{"resourceType":"AWS::EC2::Instance","resourceName":"i-0174dd38aea"}]

I want one more column IDs which will have IDS from Resource column and ll look like this:

UsernameEvent nameResourcesIDS
XYZ-DEV_ENV_POST_functionStopInstances[{"resourceType":"AWS::EC2::Instance","resourceName":"i-05fbb7a"}]i-05fbb7a"
XYZ-DEV_ENV_POST_functionStartInstances[{"resourceType":"AWS::EC2::Instance","resourceName":"i-08bd2475"},{"resourceType":"AWS::EC2::Instance","resourceName":"i-0fd69dc1"},{"resourceType":"AWS::EC2::Instance","resourceName":"i-0174dd38aea"}]i-08bd2475 , i-0fd69dc1 , i-0174

Here is output of data.head(2).to_dict():

{'Date':
{0: '28-02-2022', 1: '28-02-2022'},
'Event name':
{0: 'StopInstances', 1: 'StartInstances'},
'Resources':
{
0: '[{"resourceType":"AWS::EC2::Instance","resourceName":"i-05fbb7a"}]',
1: '[{"resourceType":"AWS::EC2::Instance","resourceName":"i-08bd2475"},{"resourceType":"AWS::EC2::Instance","resourceName":"i-0fd69dc1"},{"resourceType":"AWS::EC2::Instance","resourceName":"i-0174dd38aea"}]'},
'User name': {0: 'XYZ-DEV_ENV_POST_function', 1:
'XYZ-DEV_ENV_POST_function'}}

Thanks and Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2025-01-31 09:22:21
df['ID'] = df['Resources'].apply(lambda x: ','.join([i['resourceName'] for i in eval(x)]))

         Date  ...                                   ID
0  28-02-2022  ...                            i-05fbb7a
1  28-02-2022  ...  i-08bd2475,i-0fd69dc1,i-0174dd38aea
df['ID'] = df['Resources'].apply(lambda x: ','.join([i['resourceName'] for i in eval(x)]))

         Date  ...                                   ID
0  28-02-2022  ...                            i-05fbb7a
1  28-02-2022  ...  i-08bd2475,i-0fd69dc1,i-0174dd38aea
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文