繁体   English   中英

如何个性化Json Output以获取Pandas DataFrame和其他输出数据的列表

[英]how to personalise Json Output for a list of Pandas DataFrame and other output datas

我有以下代码,其中提供了信息

  1. 每月收到的薪金和未计入薪金的余额
  2. 检查工资是否在每个月的5号之前记入贷方,如果是,则列出工资日期。
  3. 最近3个月的平均工资

    if Salary.empty:
        total_Salary = 0
        Salary_b4_5th = "No"
        #sum_Salary = {'Salary': "-",'date': "-"}
        avg_Salary_3months = 0
        print("--------------")
        print("Is salary received before 5th :", Salary_b4_5th)
        print("--------------")
        print("No of times salary received :", total_Salary)

    else:

        Salary.date = pd.to_datetime(Salary.date, format="%d-%m-%Y")
        check_Salary_date = Salary[Salary['date'].dt.day <= 5].groupby('date').sum()
        check_Salary_date.index = check_Salary_date.index.strftime('%d-%b-%y')
        check_Salary_date.index.name = 'Date'
        sum_Salary = Salary.groupby('date').sum()
        sum_Salary = Salary.groupby(pd.Grouper(key='date', freq='1M')).sum()
        sum_Salary_3months = Salary.groupby(pd.Grouper(key='date', freq='1M')).mean().dropna(subset=['Salary']).tail(3)
        avg_Salary_3months = sum_Salary_3months['Salary'].mean()
        sum_Salary.index = sum_Salary.index.strftime('%b-%y')
        sum_Salary.index.name = 'Month'
        total_Salary = len(sum_Salary.axes[0])
        print("--------------")
        print("\nSalary received per month \n", sum_Salary)
        print("--------------")
        print("Total Count of salary received :", total_Salary)
        print("--------------")
        if not check_Salary_date.empty:
            Salary_b4_5th = "Yes"
            print("\nIs salary credited before 5th of every month:", Salary_b4_5th)
            print("--------------")
            print("List of salary credited before 5th : \n", check_Salary_date)
        else:
            Salary_b4_5th = "No"
            print("\nIs salary credited before 5th of every month:", Salary_b4_5th)
        print("--------------")
        print("Avg Salary of last 3 months :", avg_Salary_3months)
        print("--------------")

产量

Is salary credited before 5th: True
List of date where salary received before 5th :
           Balance before Salary   Salary
Date
03-Aug-18                 176.48  14783.0
04-Sep-18                  48.48  16249.0
05-Oct-18                 241.48  14448.0

Salary received per month
        Balance before Salary   Salary
Date
Jun-18                  27.20  15300.0
Jul-18                  88.20  15300.0
Aug-18                 176.48  14783.0
Sep-18                  48.48  16249.0
Oct-18                 241.48  14448.0
Nov-18                  49.48  15663.0
--------------
Avg Salary of last 3 months : 15453.333333333334
--------------

预期的Json输出:

[{"Is salary credited before 5th": "True"},

{
    "List of salary credited before 5th": {
        "Balance before Salary": {
          "03-Aug-18":176.48,
          "04-Sep-18":48.48,
          "05-Oct-18":241.48

        },
        "Salary": {
        "03-Aug-18":14783.0,
        "04-Sep-18":16249.0,
        "05-Oct-18":14448.0

        }
    }
},
{
    "Salary received per month": {
        "Balance before Salary": {
          "Jun-18":27.2,
          "Jul-18":88.2,
          "Aug-18":176.48,
          "Sep-18":48.48,
          "Oct-18":241.48,
          "Nov-18":49.48

        },
        "Salary": {
          "Jun-18":15300.0,
          "Jul-18":15300.0,
          "Aug-18":14783.0,
          "Sep-18":16249.0,
          "Oct-18":14448.0,
          "Nov-18":15663.0}
    }
},

{"Avg Salary of last 3 months" : 15453.333333333334} 
]

问题:

  1. 如何将多个DataFrame保存为Json文件。
  2. 如何将多个Non-DataFrame和数据帧另存为json文件。

简而言之,我想获得与期望的Json Output中完全相同的输出 如何获得此输出?

使用to_dict()

import json
expected_ans_1 = True
expected_ans_2 = 15453.333333333334

js = [{"Is salary credited before 5th": str(expected_ans_1)},
 {"List of salary credited before 5th": df1.to_dict()},
 {"Salary received per month": df2.to_dict()},
 {"Avg Salary of last 3 months": expected_ans_2}]

with open('test.json', 'w') as outfile:
    json.dump(js, outfile)

将用户答案存储到变量expected_ans_1expected_ans_2 在这里, df1df2是您的两个数据帧。

暂无
暂无

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

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