我有一个包含多个对象和词典的JSON,我的JSON如下:
{
"lastMonth":{
"totalCost":4120.6241,
"cpu_avg_partition":{
"10":{
"count":1,
"cost":0.0,
"value":10
"period":"lastMonth",
"resourceTypes":{
"db":{
"cost":842.8003
等......等等。
我有Python pandas代码如下所示我转换为CSV文件但不是预期的格式
Python代码:
import json,ast
import csv
import pandas as pd
from pandas import compat
compat.PY3 = True
df=pd.read_json("monthly_costformatter_data.json")
new_df = df.transpose()
column_name = new_df.columns.values
new_df[['totalCost','cpu_avg_partition','providers','period',
'resourceTypes','accounts','running_hours_partition',
'projects']].to_csv('test10pands.csv',index=True,encoding='utf-8')
输出如下CSV文件
totalCost cpu_avg_partition providers period
resourceTypes accounts running_hours_partition projects
currency_code GBP GBP GBP GBP GBP GBP GBP GBP
currentMonth 1769.9801 {u'10': {u'count': 0, u'cost': 0.0, u'value':
10}, u'100': {u'count': 0, u'cost': 0.0, u'value': 100}, u'5': {u'count':
10,
idmap {u'188f6226-59d8-4a7c-9fdb-f32f4f0885ff':
u'628455167342'} {u'813': u'ACP PM Work'}
lastMonth 4120.6241 {u'10': {u'count': 1, u'cost': 0.0, u'value': 10},
u'100': {u'count': 0, u'cost': 0.0, u'value': 100}, u'5': {u'count': 16,
u'cost': 0.0, u'value': 5}, u'50': {u'count': 0, u'cost': 0.0, u'value': 50},
但我也使用
删除了你的unicode字符