![](/img/trans.png)
[英]how to make pandas isin output dataframe into json or list format
[英]how to personalise Json Output for a list of Pandas DataFrame and other output datas
我有以下代码,其中提供了信息
码
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}
]
问题:
简而言之,我想获得与期望的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_1
和expected_ans_2
。 在这里, df1
和df2
是您的两个数据帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.