如何删除数据帧 Scala/sSark 中的前几行? [英] How to delete the first few rows in dataframe Scala/sSark?

查看:29
本文介绍了如何删除数据帧 Scala/sSark 中的前几行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataFrame,我想删除第一行和第二行.我该怎么办?

I hava a DataFrame and I want to delete first and the second row. What should I do?

这是我的输入:

+-----+
|value|
+-----+
|    1|
|    4|
|    3|
|    5|
|    4|
|   18|
-------

这是异常结果:

+-----+
|value|
+-----+
|    3|
|    5|
|    4|
|   18|
-------

推荐答案

在我看来,如果您无法定义数据框的顺序,那么谈论第一条或第二条记录是没有意义的.show 语句导致的记录排序是任意的",取决于数据的分区.

In my opinion it does not make sense to speak about a first or second record if you cannot define an ordering of your dataframe. The ordering of the records as a result of the show statement is "arbitrary" and depends on partitioning of your data.

假设您有一列可以对记录进行排序,您可以使用窗口函数.从此数据框开始:

Suppose you have a column over which you can order your records, you can use Window-functions. Starting with this dataframe:

+----+-----+
|year|value|
+----+-----+
|2007|    1|
|2008|    4|
|2009|    3|
|2010|    5|
|2011|    4|
|2012|   18|
+----+-----+ 

你可以做到

import org.apache.spark.sql.expressions.Window

df
.withColumn("rn",row_number().over(Window.orderBy($"year")))
.where($"rn">2).drop($"rn")
.show

这篇关于如何删除数据帧 Scala/sSark 中的前几行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆