Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为熊猫数据帧和其他输出数据列表个性化Json输出_Python_Json_Pandas - Fatal编程技术网

Python 如何为熊猫数据帧和其他输出数据列表个性化Json输出

Python 如何为熊猫数据帧和其他输出数据列表个性化Json输出,python,json,pandas,Python,Json,Pandas,我有下面的代码,它提供了信息 每月收到的工资和工资贷记前的余额 检查是否在每月5日前将工资记入贷方,如果是,则列出带日期的工资清单 过去3个月的平均工资 代码 if Salary.empty: total_Salary = 0 Salary_b4_5th = "No" #sum_Salary = {'Salary': "-",'date': "-"} avg_Salary_3months = 0 print(

我有下面的代码,它提供了信息

  • 每月收到的工资和工资贷记前的余额
  • 检查是否在每月5日前将工资记入贷方,如果是,则列出带日期的工资清单
  • 过去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("--------------")
    
    预期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} 
    ]
    
    问题:

  • 如何将多个数据帧保存为Json文件
  • 如何将多个非数据帧与数据帧一起保存为json文件
  • 简而言之,我希望得到一个与预期Json输出中所给出的完全相同的输出。如何获得此输出?

    使用
    来记录()

    将用户答案存储到变量
    预期值\u ans\u 1
    预期值\u ans\u 2
    中。
    这里,
    df1
    df2
    是您的两个数据帧

    在预期输出的第一个元素中,
    True
    如何转换为
    yes
    !对不起,这是
    真的
    。我已经改变了我将如何将其放入这个模型:
    df.to_json(“/path/test.json”)
    我将在哪里序列化这些数据?我们如何创建这种格式
    [{“5号之前的工资是否已计入”:str(预期值)},{“5号之前计入的工资列表”:df1.to_dict()},{“每月收到的工资”:df2.to_dict()},{”过去3个月的平均工资:预期2}]
    您能告诉我如何创建该格式吗?以及如何将其保存为本地系统中的json文件我已根据您的需要更新了答案以保存到json文件中。创建该格式需要所述的嵌套字典列表。我需要您的帮助,当数据帧
    df1.to_dict()
    df2.to_dict()时,我遇到了一个错误
    为空。有什么办法可以解决这个问题吗
    [{"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} 
    ]
    
    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)