定义熊猫数据框的特定json导出格式

时间:2019-09-13 19:01:19

标签: python json pandas dataframe export

我需要将DF导出为特定的JSON格式,但是我正在努力以正确的方式对其进行格式化。

我想用shop_details创建一个小节,该小节将显示商店的城市和位置(如果已知),否则应将其留空。

DF的代码:

from pandas import DataFrame

Data = {'item_type': ['Iphone','Computer','Computer'],
        'purch_price': [1200,700,700],
        'sale_price': [1150,'NaN','NaN'],
        'city': ['NaN','Los Angeles','San Jose'],
        'location': ['NaN','1st street', '2nd street']
        }

DF看起来像这样:

  item_type  purch_price sale_price         city    location
0    Iphone         1200       1150          NaN         NaN
1  Computer          700        NaN  Los Angeles  1st street
2  Computer          700        NaN     San Jose  2nd street

输出格式应如下所示:

[{
    "item_type": "Iphone",
    "purch_price": "1200",
    "sale_price": "1150",
    "shop_details": []
  },
  {
    "item_type": "Computer",
    "purch_price": "700",
    "sale_price": "600",
    "shop_details": [{
        "city": "Los Angeles",
        "location": "1st street"
      },
      {
        "city": "San Jose",
        "location": "2nd street"
      }
    ]
  }
]

2 个答案:

答案 0 :(得分:0)

import json

df = df.fillna('')

def shop_details(row):
    if row['city'] != '' and row['location'] !='':
        return [{'city': row['city'], 'location': row['location']}]
    else:
        return []

df['shop_details'] = df.apply(lambda row: shop_details(row), axis = 1)

df = df.drop(['city', 'location'], axis = 1)

json.dumps(df.to_dict('records'))

唯一的问题是我们不按item_type分组,但是您应该做一些工作;)

答案 1 :(得分:0)

您可以按照以下方式来实现输出。谢谢

from pandas import DataFrame

Data = {'item_type': ['Iphone','Computer','Computer'],
        'purch_price': [1200,700,700],
        'sale_price': [1150,'NaN','NaN'],
        'city': ['NaN','Los Angeles','San Jose'],
        'location': ['NaN','1st street', '2nd street']
        }

df = DataFrame(Data, columns= ['item_type', 'purch_price', 'sale_price', 'city','location' ])

Export = df.to_json ('path where you want to export your json file')