繁体   English   中英

窗口中的数据框中的Python Boto3列表实例

[英]Python Boto3 List Instances in a Dataframe Windowed

我正在尝试在Boto3的表中列出我的EC2实例。

instances = ec2.instances.filter(
    Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
instance_count = sum(1 for _ in instances.all())
RunningInstances = []
for instance in instances:
    id = instance.id
    name = self.get_name_tag(instance)
    type = instance.instance_type
    mylist = {"name":name,"id":id,"type":type}
    RunningInstances.append(mylist.copy())
import numpy as np
import pandas as pd
table = pd.DataFrame.from_dict(RunningInstances)
print(table)

这将创建一个表格-太好了! 我的问题是,即使列可以容纳在当前窗口中,列也会溢出到另一行:

                 id                                           name  \
0            i-56b243ge                                       My Really Long Name for my Instance
1            i-89b789ga                                       My 2nd Really Long Name for my Instance

                 type
0            t2-micro
1            t2-small

我刚刚了解到熊猫现在已经存在,所以我还没有完全掌握样式。 请记住,名称列标题位于名称值的右侧-由于Stack的格式,我不能将其放在此处。 如何将所有数据框/表放入一个视图?

欢迎来到熊猫世界,您一定会喜欢它在数据框上执行操作的方式,而不会期望您迭代这些值。 我对您的解析进行了一些更改,以演示其中的一些功能!

import boto3
import pandas as pd
client=boto3.client('ec2')
response = client.describe_instances()
df=pd.io.json.json_normalize(
    pd.DataFrame(
        pd.DataFrame(
            response['Reservations']
        )\
        .apply(lambda r: r.str[0])\. # Get only the first element of the list
        .Instances\
        .values
        )[0]
    )[['ImageId','InstanceType','Tags']]
df['Name']=df.Tags.apply(lambda r: r[0]['Value']) # Get the name from the tags dict
df.drop(['Tags'], axis=1, inplace=True)
df

会给你:

    ImageId InstanceType Name
0   ami-123 t2.2xlarge   name1
1   ami-234 t2.large     name2
2   ami-345 m5.12xlarge  name3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM