简体   繁体   English

将熊猫df写入xlsx时,如何使此循环正常工作?

[英]How do I get this loop to work correctly when writing pandas df to xlsx?

I have used this code , which is kind of working. 我已经使用了这段代码 ,这是可行的。 Right now in the smaller 'rep_list' as it executes the first rep in the list which is CP is adds it, but then when it goes to AM it overwrites the CP. 现在,在较小的“ rep_list”中,它执行列表中的第一个rep(即CP)并将其添加,但是当它到达AM时,它将覆盖CP。 SO right now when I run this code it only actually saves the last person in the loop. 因此,当我运行此代码时,它实际上仅将最后一个人保存在循环中。 If I run the code with just "CP" and then just "AM" it appends it as it should. 如果我仅使用“ CP”运行代码,然后仅使用“ AM”运行代码,则它会按需添加。 Is it something wrong with the for loop? for循环有问题吗? or is it an issue with the workbook itself? 还是工作簿本身存在问题?

import pandas as pd
import datetime
from openpyxl import load_workbook

now = datetime.datetime.now()
currentDate = now.strftime("%Y-%m-%d")
call_report = pd.read_excel("Ending 2016-07-30.xlsx", "raw_data")

#rep_list = ["CP", "AM", "JB", "TT", "KE"]
rep_list = ["CP", "AM"]

def call_log_reader(rep_name):
    rep_log = currentDate + "-" + rep_name + ".csv"
    df = pd.read_csv(rep_log)
    df = df.drop(['From Name', 'From Number', 'To Name / Reference', 'To Number', 'Billing Code', 'Original Dialed Number',
     'First Hunt Group', 'Last Hunt Group'], axis=1)
    df['rep'] = rep_name

    book = load_workbook('Ending 2016-07-30.xlsx')
    writer = pd.ExcelWriter('Ending 2016-07-30.xlsx', engine='openpyxl')
    writer.book = book
    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
    df.to_excel(writer, "raw_data", index=False)
    writer.save()
    ## I tried adding this : writer.close() hoping it would close the book and then force it to reopen for the next rep in the loop but it doesn't seem to work.

for rep in rep_list:
    call_log_reader(rep)

Thank you so much! 非常感谢!

EDIT: 编辑:

Gaurav Dhama gave a great answer that worked excellent. Gaurav Dhama给出了一个很好的答案,效果很好。 He pointed out that there is a bit of a limitation with the Pandas excelwriter (refer to this link) and proposed a solution in which each rep gets their own sheet in the end. 他指出,Pandas excelwriter有一个局限性(请参阅此链接),并提出了一个解决方案,在该解决方案中,每个代表最后都有自己的工作表。 This worked, however after I thought on it I opted against the additional sheets and came up with this solution knowing the limitation existed. 这行得通,但是在考虑之后,我选择了其他表格,并在知道存在限制的情况下提出了此解决方案。 Basically, I appended a CSV instead of the actual XLSX file, and then at the end opened that CSV and appended the one big list into the XLSX file. 基本上,我附加了一个CSV而不是实际的XLSX文件,然后最后打开了该CSV并将一个大列表附加到了XLSX文件中。 Either one works, just depends on what you're final product looks like. 哪种方法都有效,仅取决于最终产品的外观。

import pandas as pd
import datetime
from openpyxl import load_workbook

now = datetime.datetime.now()
currentDate = now.strftime("%Y-%m-%d")
call_report = "Ending 2016-07-30.xlsx"
#rep_list = ["CP", "AM", "JB", "TT", "KE"]
rep_list = ["CP", "AM"]
csv_to_xl_files = []
merged_csv = currentDate + "-master.csv"

def call_log_reader(rep_name):
    rep_log = currentDate + "-" + rep_name + ".csv"
    df = pd.read_csv(rep_log)
    df = df.drop(['TimestampDetail', 'Billing Code', 'From Name', 'From Number', 'To Name / Reference', 'To Number',
              'Original Dialed Number', 'First Hunt Group', 'Last Hunt Group'], axis=1)
    df['rep'] = rep_name
    #print (df.head(3))
    df.to_csv(merged_csv, mode='a', index=False, header=False)
    csv_to_xl_files.append(rep_log)

book = load_workbook(call_report)
writer = pd.ExcelWriter(call_report, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

for rep in rep_list:
    call_log_reader(rep)

master_df = pd.read_csv(merged_csv)
master_df.to_excel(writer, "raw_data", index=False)
writer.save()

#this csv_to_xl_files list isn't finished yet, basically I'm going to use it to delete the files from the directory as I don't need them once the script is run.
print (csv_to_xl_files)

Try using the following: 尝试使用以下内容:

import pandas as pd
import datetime
from openpyxl import load_workbook

now = datetime.datetime.now()
currentDate = now.strftime("%Y-%m-%d")
call_report = pd.read_excel("Ending 2016-07-30.xlsx", "raw_data")

#rep_list = ["CP", "AM", "JB", "TT", "KE"]
rep_list = ["CP", "AM"]

def call_log_reader(rep_name):
    rep_log = currentDate + "-" + rep_name + ".csv"
    df = pd.read_csv(rep_log)
    df = df.drop(['From Name', 'From Number', 'To Name / Reference', 'To Number', 'Billing Code', 'Original Dialed Number',
     'First Hunt Group', 'Last Hunt Group'], axis=1)
    df['rep'] = rep_name
    df.to_excel(writer, "raw_data"+rep, index=False)
    return df

book = load_workbook('Ending 2016-07-30.xlsx')
writer = pd.ExcelWriter('Ending 2016-07-30.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

for rep in rep_list:
    call_log_reader(rep)

writer.save()

如果使用openpyxl 2.4,则可以直接在openpyxl中使用Pandas数据帧

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

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