简体   繁体   English

使用 pandas to_csv 将结果写入 csv

[英]using pandas to_csv to write the result to a csv

Below is my code which gets the instance name from excel sheet and searches for the instance name in aws account and gets the instance id.下面是我的代码,它从 excel 表中获取实例名称,并在 aws 帐户中搜索实例名称并获取实例 ID。 So that process is working fine.所以这个过程运行良好。 now i wanted to write the data to csv say like in a csv there has to be two columns with names like instancename and instanceId and the data should be printed in respective columns.现在我想将数据写入 csv 就像在 csv 中一样,必须有两列名称如 instancename 和 instanceId,并且数据应打印在各自的列中。 please help me in getting the desired output.请帮助我获得所需的 output。 the csv is overlapped with the data and once opening the csv i can just see the last values in it because my script overrides the previous results. 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')

Expected output:预期 output:

Instancename  InstanceID
testinstance  123456
testinstance1 12345656312
testinstance2 12345657237

Actual output:实际 output:

Instancename  InstanceID
testinstance2 12345657237

You can use pandas concat or append but best way is to store the data into list and in the end make a dataframe of that and save it as csv. You can use pandas concat or append but best way is to store the data into list and in the end make a dataframe of that and save it as 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')

Your code makes a new 'data' variable for every iteration of the for loop.您的代码为 for 循环的每次迭代创建一个新的“数据”变量。 My attempt is making a blank data variable before starting the loop.我的尝试是在开始循环之前制作一个空白数据变量。 Add a new piece to the dataframe each loop, and once you're out of the loop you save it to csv在每个循环中添加一个新的 dataframe,一旦你退出循环,你将它保存到 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')

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

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