我在 PySpark 中有一个名为 avg_length_df
的 Spark DataFrame,它看起来像这样 -
+----------------+---------+----------+-----------+---------+-------------+----------+
| id | x| a| b| c| country| param|
+----------------+---------+----------+-----------+---------+-------------+----------+
| 40.0| 9.0| 5.284| 5.047| 6.405| 13.0|avg_length|
+----------------+---------+----------+-----------+---------+-------------+----------+
我想将它从行转置为列,使其变成 -
+----------+
|avg_length|
+----------+
| 40.0|
| 9.0|
| 5.284|
| 5.047|
| 6.405|
| 13.0|
+----------+
接下来,我有第二个数据框df2
:
+----------------+------+
| col_names|dtypes|
+----------------+------+
| id|string|
| x| int|
| a|string|
| b|string|
| c|string|
| country|string|
+----------------+------+
我想在
df2
中创建一个名为avg_length
的列,其值应等于上述转置的DataFrame。因此,预期输出如下:+----------------+------+----------+
| col_names|dtypes|avg_length|
+----------------+------+----------+
| id|string| 40.0|
| x| int| 9.0|
| a|string| 5.284|
| b|string| 5.047|
| c|string| 6.405|
| country|string| 13.0|
+----------------+------+----------+
我该如何完成这两个操作?