Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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中的pivot数据帧_Python_Pyspark_Apache Spark Sql - Fatal编程技术网

Python pyspark中的pivot数据帧

Python pyspark中的pivot数据帧,python,pyspark,apache-spark-sql,Python,Pyspark,Apache Spark Sql,我有DF测试包含以下列 Type Name Country Year Value 1 Rec US 2018 8 2 fg UK 2019 2 5 vd India 2020 1 7 se US 2021 3 我想在它上面做一个支点,我试过下面的表达 pivotdata=spark.sql(“从测试中选择*)

我有DF测试包含以下列

Type  Name  Country      Year    Value
1     Rec      US        2018      8
2     fg       UK        2019      2
5     vd      India      2020      1
7     se       US        2021      3
我想在它上面做一个支点,我试过下面的表达
pivotdata=spark.sql(“从测试中选择*).groupby(“国家”).pivot(“年度”).sum(“值”).show()

我得到了输出,但除了剩下的两列之外,它只显示了几列

Country  2018  2019  2020  2021
US        -     -
UK        -      -
India     -      -
US        -      -

因此,如果我想要所有列,我们该怎么办?如果我正确理解了您的需要,您必须将其他列也输入sum()中。考虑下面的例子:

tst=sqlContext.createDataFrame([('2020-04-23',1,2,"india"),('2020-04-24',1,3,"india"),('2020-04-23',1,4,"china"),('2020-04-24',1,5,"china"),('2020-04-23',1,7,"germany"),('2020-04-24',1,9,"germany")],schema=('date','quantity','value','country'))
tst.show()
+----------+--------+-----+-------+
|      date|quantity|value|country|
+----------+--------+-----+-------+
|2020-04-23|       1|    2|  india|
|2020-04-24|       1|    3|  india|
|2020-04-23|       1|    4|  china|
|2020-04-24|       1|    5|  china|
|2020-04-23|       1|    7|germany|
|2020-04-24|       1|    9|germany|
+----------+--------+-----+-------+
df_pivot=tst.groupby('country').pivot('date').sum('quantity','value').show()
df_pivot.show()
+-------+------------------------+---------------------+------------------------+---------------------+
|country|2020-04-23_sum(quantity)|2020-04-23_sum(value)|2020-04-24_sum(quantity)|2020-04-24_sum(value)|
+-------+------------------------+---------------------+------------------------+---------------------+
|germany|                       1|                    7|                       1|                    9|
|  china|                       1|                    4|                       1|                    5|
|  india|                       1|                    2|                       1|                    3|
+-------+------------------------+---------------------+------------------------+---------------------+
如果您不喜欢有趣的列名,那么可以使用agg函数为数据透视列名定义自己的后缀

tst_res=tst.groupby('country').pivot('date').agg(F.sum('quantity').alias('sum_quantity'),F.sum('value').alias('sum_value'))
tst_res.show()
+-------+-----------------------+--------------------+-----------------------+--------------------+
|country|2020-04-23_sum_quantity|2020-04-23_sum_value|2020-04-24_sum_quantity|2020-04-24_sum_value|
+-------+-----------------------+--------------------+-----------------------+--------------------+
|germany|                      1|                   7|                      1|                   9|
|  china|                      1|                   4|                      1|                   5|
|  india|                      1|                   2|                      1|                   3|
+-------+-----------------------+--------------------+-----------------------+--------------------+

除了剩下的两行之外,你所说的
是什么意思?我希望在你发布的输出中也有Type和name列,如果你按“country”分组,你应该只有3行。为什么国家/地区“us”重复出现?请发布相应的预期输出。@codetech-您是否试用了该解决方案?成功了吗?很高兴听到:-)