如何在xlsx文件中获取当前行并将其除以前一行?

时间:2016-05-20 12:20:35

标签: python excel pandas xlsxwriter

我正在使用此classxlsx个文件中编写统计信息:

class DictWriter:
    def __init__(self, workbook, worksheet, fieldnames):
        self.workbook = workbook
        self.worksheet = worksheet
        self.fieldnames = fieldnames
        self.nrow = 1

    def writerow(self, d, condition=0):
        for k in d:
            ncol = self.fieldnames.index(k)
            self.worksheet.write(self.nrow, ncol, d[k])
            self.nrow += 1

    def writeheader(self):
        for ncol, fieldname in enumerate(self.fieldnames):
            self.worksheet.write(0, ncol, fieldname)

以这种方式使用它:

def file_generator():
    link = "http://linkforexample.com/api/getSomething"
    client = requests.get(link)
    response = client.json()['response']
    if response['httpStatus'] == 200:
        workbook = xlsxwriter.Workbook('Statistics.xlsx', {'strings_to_numbers': True})
        worksheet = workbook.add_worksheet()
        data_table = response['data']['data']
        fieldnames = ["Date", "Advertiser", "Clicks", "Conversions", "Cost", "Revenue", "Profit"]
        writer = DictWriter(workbook, worksheet, fieldnames)
        writer.writeheader()

        for d in data_table:
            warning = (float(real_profit)/float(d['Stat']['revenue']))*100
            row_dict = {"Date": d['Stat']['date'], "Advertiser": d['Advertiser']['company'], "Clicks": d['Stat']['clicks'], "Conversions": d['Stat']['conversions'], "Cost": d['Stat']['payout'], "Revenue": d['Stat']['revenue'], "Profit": d['Stat']['profit']}
            writer.writerow(row_dict)

如何创建一个带有n-1值的column,这些值将包含行X中当前单元格中的值与前一个值之间的差异?

谢谢!

1 个答案:

答案 0 :(得分:2)

使用pd.read_excel()pd.read_json()docs)将您的Excel文件/ json输入读入pandas.DataFrame,然后使用第一个值的差异创建一个新列像这样的现有列(docs):

df['new_col'] = df[['existing_col']].diff()