使用pandas to_csv将结果写入csv

时间:2019-11-20 21:12:24

标签: python pandas export-to-csv

下面是我的代码,该代码从excel工作表获取实例名称,并在aws帐户中搜索实例名称并获取实例ID。因此,该过程运行良好。现在我想将数据写入csv,就像在csv中一样,必须有两列,其名称必须如instancename和instanceId,并且数据应打印在相应的列中。请帮助我获得所需的输出。 csv与数据重叠,一旦打开csv,我只能看到其中的最后一个值,因为我的脚本会覆盖以前的结果。

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import numpy as np
import os
import boto3
client = boto3.client('ec2')
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, 'example.xlsx')

df = pd.read_excel(my_file, sheet_name='Sheet2')

list1 = df['EC2NAMES']
print(list1)
client = boto3.client('ec2')
for names in list1:
    custom_filter = [{
        'Name':'tag:Name', 
        'Values': [names]}]
    print(names)
    instances = client.describe_instances(Filters=custom_filter)
    for instance in instances['Reservations']:
        for key in instance["Instances"]:
            x = key['InstanceId']
            print(x)
            data = pd.DataFrame({'A' : [names],'B' : [x]})
            data.to_csv('df111111.csv')

预期输出:

Instancename  InstanceID
testinstance  123456
testinstance1 12345656312
testinstance2 12345657237

实际输出:

Instancename  InstanceID
testinstance2 12345657237

2 个答案:

答案 0 :(得分:2)

您可以使用熊猫concatappend,但最好的方法是将数据存储到列表中,最后制作一个数据框并将其保存为csv。

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import numpy as np
import os
import boto3
client = boto3.client('ec2')
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, 'example.xlsx')

df = pd.read_excel(my_file, sheet_name='Sheet2')

list1 = df['EC2NAMES']
print(list1)
client = boto3.client('ec2')
data = []
for names in list1:
    custom_filter = [{
        'Name':'tag:Name', 
        'Values': [names]}]
    print(names)
    instances = client.describe_instances(Filters=custom_filter)
    for instance in instances['Reservations']:
        for key in instance["Instances"]:
            x = key['InstanceId']
            print(x)
            data.append([names, x])
pd.DataFrame(data, colums=['A','B']).to_csv('df111111.csv')

答案 1 :(得分:1)

您的代码为for循环的每次迭代创建一个新的“数据”变量。我的尝试是在开始循环之前制作一个空白数据变量。在每个循环中向数据帧添加一个新片段,一旦退出循环,将其保存到csv

data = pd.DataFrame()
list1 = df['EC2NAMES']
print(list1)
client = boto3.client('ec2')
for names in list1:
    custom_filter = [{
        'Name':'tag:Name', 
        'Values': [names]}]
    print(names)
    instances = client.describe_instances(Filters=custom_filter)
    for instance in instances['Reservations']:
        for key in instance["Instances"]:
            x = key['InstanceId']
            print(x)
            data = data.append(pd.DataFrame({'A' : [names],'B' : [x]}))
data.to_csv('df111111.csv')