我有一个像这样的Pypark数据框
+----------+------+------+------+------+------+------+------+------+------+------+------+------+------+
|id |201806|201807|201808|201809|201810|201811|201812|201901|201902|201903|201904|201905|201906|
+----------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| 1 | 15| 15| 15| 15| 15| 15| 15| 15| 15| null| 15| 15| 15|
| 2 | 4| 4| 4| 4| 4| 4| 4| 4| 4| 4| 4| 4| 4|
| 3 | 7| 7| 7| 7| 7| 7| 7| 7| null| null| null| null| null|
-------------------------------------------------------------------------------------------------------
我想从这些数据中找出每一行最新的非空值。你知道吗
我期望得到以下结果。你知道吗
+----------+------+
|id. |latest|
+----------+------+
| 1 | 15|
| 2 | 4|
| 3 | 7|
-------------------
我遵循了这个answer,但是我不能对每行执行操作。你知道吗
我用过
df.select([last(x, ignorenulls=True).alias(x) for x in df.columns])
但这段代码只按列执行,我希望按行执行相同的操作。你知道吗
假设您的列是从最早到最新排序的,您可以使用下面的代码使用
coalesce
来获取最新的值。你知道吗输出:
相关问题 更多 >
编程相关推荐