Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pyspark中使用nestes数组将dataframe转换为dict_Python_Pandas_Dataframe_Pyspark_Pandas Groupby - Fatal编程技术网

Python 如何在pyspark中使用nestes数组将dataframe转换为dict

Python 如何在pyspark中使用nestes数组将dataframe转换为dict,python,pandas,dataframe,pyspark,pandas-groupby,Python,Pandas,Dataframe,Pyspark,Pandas Groupby,我有以下数据框销售数据: id year month total_sales 0 2020 1 200 1 2019 12 866474119 1 2019 10 555 1 2019 11 13073203 1 2020 2 5255259695 1 2020 1 13622027370 从这里,我想做一个词汇,如下所示: [ { "2020"

我有以下数据框销售数据:

id  year    month   total_sales
0   2020    1       200
1   2019    12      866474119
1   2019    10      555
1   2019    11      13073203
1   2020    2       5255259695
1   2020    1       13622027370
从这里,我想做一个词汇,如下所示:

[
  {
    "2020": {
      "1": "200"
    },
    "id": "0"
  },
  {
    "2019": {
      "10": "555",
      "11": "13073203",
      "12": "866474119"
    },
    "2020": {
      "1": "13553473101",
      "2": "6000"
    },
    "id": "1"
  }
]

我将df转换为我想要知道的输出,而不转换如何实现该输出

我认为即使在pyspark中,您也必须使用collect()到驱动程序节点,然后在行列表中使用asDict()。用熊猫的方式做可能是你最好的选择。我可能错了..您可以在分组中为k,g引用:d={k:recurdictify(g.ix[:,1:])^我正在获取语法错误:无效语法