Reputation: 115
i have grouped the set of results and trying to convert that results to python with to_json method.
This is my code to convert my results to json. The results variable contains all the results.
results = {'main_category' : {'total_count' : total_count,'total_predicted_postive_count' : total_predicted_postive_count, 'total_predicted_negative_count' : total_predicted_negative_count ,
'total_predicted_postive_rate' : total_predicted_postive_rate, 'total_predicted_negative_rate' : total_predicted_negative_rate, 'high_predicted_positive_region' : high_predicted_positive_region,
'model_accuracy' : model_accuracy } }, { 'category' : [{ 'subcategory' : 'gender'}, [{'subcategory_name' : [{ 'subcategory_name' : 'male', 'churn_count' : male_churn_count, 'churn_rate' : male_churn_rate, 'retention_count' : male_retention_count,
'retention_rate' : male_retention_rate }] }] ] }
overall_results = pd.Series(results).to_json(orient='records')
print(overall_results)
After i convert this to json, my output will be like this
[
{
"main_category": {
"high_predicted_positive_region": 564,
"total_predicted_postive_count": 1481,
"model_accuracy": 1,
"total_predicted_postive_rate": 0.2212429041,
"total_predicted_negative_rate": 0.7787570959,
"total_count": 7032,
"total_predicted_negative_count": 5213
}
},
{
"category": [
{
"subcategory": "gender"
},
[
{
"subcategory_name": [
{
"churn_rate": 0.106363908,
"churn_count": 712,
"retention_count": 2658,
"subcategory_name": "male",
"retention_rate": 0.3970720048
}
]
}
]
]
}
]
But my expected output format should be like this
[
{
"main_category": {
"high_predicted_positive_region": 564,
"total_predicted_postive_count": 1481,
"model_accuracy": 1,
"total_predicted_postive_rate": 0.2212429041,
"total_predicted_negative_rate": 0.7787570959,
"total_count": 7032,
"total_predicted_negative_count": 5213
}
},
{
"category": [
{
"subcategory": "gender"
},
[
{
"subcategory_name": [
{
"subcategory_name": "male",
"churn_count": 712,
"churn_rate": 0.106363908,
"retention_count": 2658,
"retention_rate": 0.3970720048
}
]
}
]
]
}
]
inside the array of subcategory name, whatever order i mentioned in the results, it should display. But i am getting in a mixed order.
Where should i change according to that order. Any other way to get this same result format.
Upvotes: 0
Views: 73
Reputation: 491
The json.dumps() has a sorted key that when set to True will output a sorted json output. You could insert a json.dumps() call with sorted=True. (not sure what orient='records' does but you would lose this).
Be warned though that this will not work on python3 as the mixed-mode < operator has been removed and you will get an error if your keys have different types.
Generally dicts are unordered...
Otherwise you must write your own dict parser....
Upvotes: 0
Reputation: 728
In Python, dictionaries are not ordered. If you want to preserve the insertion order, you need to use an OrderedDict.
It seems you're using Pandas. I don't know much about it, but this topic will help you further with using OrderedDicts in Pandas.
Upvotes: 1