python - 如何将 pandas DataFrame 结果转换为用户定义的 json 格式

标签 python json pandas dataframe

data_df = pandas.read_csv('details.csv')
data_df = data_df.replace('Null', np.nan)
df = data_df.groupby(['country', 'branch']).count()
df = df.drop('sales', axis=1)  
df = df.reset_index()
print df

我想将数据帧(df)的结果转换为我下面提到的用户定义的 json 格式。打印结果(df)后,我将得到表单中的结果

country     branch      no_of_employee     total_salary    count_DOB   count_email
  x            a            30                 2500000        20            25
  x            b            20                 350000         15            20
  y            c            30                 4500000        30            30
  z            d            40                 5500000        40            40
  z            e            10                 1000000        10            10
  z            f            15                 1500000        15            15

我想将其转换为 Json。我想要的格式是

x
   {
      a
        {
              no.of employees:30
              total salary:2500000
              count_email:25
         }
       b
         {
              no.of employees:20
              total salary:350000
              count_email:25

           }
     }

   y
     {

        c
         {
              no.of employees:30
              total salary:4500000
              count_email:30

           }
      }
   z
     {
       d
         {
              no.of employees:40
              total salary:550000
              count_email:40
         }
       e
         {
              no.of employees:10
              total salary:100000
              count_email:15

         }
        f
         {
              no.of employees:15
              total salary:1500000
              count_email:15

         }
    }

请注意,我不希望数据帧结果中的所有字段都为 Json(例如:count_DOB)

最佳答案

您可以使用groupbyapply to_dict最后to_json :

  country branch  no_of_employee  total_salary  count_DOB  count_email
0       x      a              30       2500000         20           25
1       x      b              20        350000         15           20
2       y      c              30       4500000         30           30
3       z      d              40       5500000         40           40
4       z      e              10       1000000         10           10
5       z      f              15       1500000         15           15

g = df.groupby('country')[["branch", "no_of_employee","total_salary", "count_email"]]
                              .apply(lambda x: x.set_index('branch').to_dict(orient='index'))
print g.to_json()
{
    "x": {
        "a": {
            "total_salary": 2500000,
            "no_of_employee": 30,
            "count_email": 25
        },
        "b": {
            "total_salary": 350000,
            "no_of_employee": 20,
            "count_email": 20
        }
    },
    "y": {
        "c": {
            "total_salary": 4500000,
            "no_of_employee": 30,
            "count_email": 30
        }
    },
    "z": {
        "e": {
            "total_salary": 1000000,
            "no_of_employee": 10,
            "count_email": 10
        },
        "d": {
            "total_salary": 5500000,
            "no_of_employee": 40,
            "count_email": 40
        },
        "f": {
            "total_salary": 1500000,
            "no_of_employee": 15,
            "count_email": 15
        }
    }
}

我尝试print g.to_dict(),但 JSON 无效(检查 here )。

关于python - 如何将 pandas DataFrame 结果转换为用户定义的 json 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35473711/

相关文章:

python - 从 Django 模板获取 URL 的第一部分

python - 如何将列合并为 Python 中的单个列?

python - 在 C++ 中为 Tensorflow 模型定义一个 feed_dict

javascript - 从 Siemens S7 1500 PLC 上的 Web 服务器页面读取 JSON 结构

python - 导出 pandas DataFrame 时如何删除列名行?

python - 如何使用 python 和 pandas 将单个 `"` 放入我的 csv 中

python - Pandas 使用逻辑或公共(public)列之间的方式合并两个数据框

Python基础——重命名文件

javascript - 确保不存在重复的目录路径

javascript - 解析并获取 JSON 数据