【问题标题】:Excel report using python pandas for historical data使用 python pandas 获取历史数据的 Excel 报告
【发布时间】:2018-01-31 03:28:56
【问题描述】:

我想使用 python pandas 生成一个 excel 报告。 我有如下客户的 json 数据,并且“id”是唯一的。

customer_day1 = [{"id": "1","name": "John","ip": "10.1.1.1"},
                 {"2": "Peter","name": "ip": "10.1.1.2"}]
customer_day2 = [{"id": "1","name": "John","ip": "10.1.1.10"}, 
                 {"3": "Nancy","name": "ip": "10.1.1.3"}]

想要生成具有以下详细信息的 excel 报告

  1. 突出显示新客户行
  2. 突出显示已删除的客户
  3. 突出显示客户在两个日期之间更改了值

需要识别2个日期数据的差异并生成包含上述详细信息的报告。

【问题讨论】:

  • 我认为这不是一个好习惯。您可能需要先将数据存储到数据库中,然后您可以根据日期获取数据并找出。否则,您将不得不在名为“日期”的数据中添加新列。

标签: python pandas dataframe


【解决方案1】:

我可以使用 pandas 数据框找到不同之处。参考http://pbpython.com/excel-diff-pandas.html

import pandas as pd
import numpy as np

def report_diff(x):
    return x[0] if x[0] == x[1] else '{} ---> {}'.format(*x)

def has_change(row):
    if "--->" in row.to_string():
        return "Y"
    else:
        return "N"

customer_day1 = '[{"id": "1","name": "John","ip": "10.1.1.1"},{"id":"2", "name":"Peter", "ip": "10.1.1.2"}]'
customer_day2 = '[{"id": "1","name": "John","ip": "10.1.1.10"},{"id": "3", "name":"Nancy", "ip": "10.1.1.3"}]'

df1 = pd.read_json(customer_day1)
df2 = pd.read_json(customer_day2)
df1.set_index('id',inplace=True)
df2.set_index('id',inplace=True)

df_panel = pd.Panel(dict(df1=df1,df2=df2))
df_output = df_panel.apply(report_diff, axis=0)
df_output['has_change'] = df_output.apply(has_change, axis=1)


writer = pd.ExcelWriter("Report_1.xlsx",engine='xlsxwriter')
df_output.to_excel(writer,"report")    
writer.save()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多