最初,我误会了,并以为您想分割这些列。如果要选择行的子集,一种方法是使用创建索引列
monotonically_increasing_id()。从文档:
保证生成的ID是单调递增且唯一的,但不是连续的。
您可以使用此ID对数据框进行排序,并使用该ID对其子集进行排序,
limit()以确保准确获得所需的行。
例如:
import pyspark.sql.functions as fimport string# create a dummy df with 500 rows and 2 columnsN = 500numbers = [i%26 for i in range(N)]letters = [string.ascii_uppercase[n] for n in numbers]df = sqlCtx.createDataframe( zip(numbers, letters), ('numbers', 'letters'))# add an index columndf = df.withColumn('index', f.monotonically_increasing_id())# sort ascending and take first 100 rows for df1df1 = df.sort('index').limit(100)# sort descending and take 400 rows for df2df2 = df.sort('index', ascending=False).limit(400)只是为了验证这是否符合您的要求:
df1.count()#100df2.count()#400
我们还可以验证索引列是否不重叠:
df1.select(f.min('index').alias('min'), f.max('index').alias('max')).show()#+---+---+#|min|max|#+---+---+#| 0| 99|#+---+---+df2.select(f.min('index').alias('min'), f.max('index').alias('max')).show()#+---+----------+#|min| max|#+---+----------+#|100|8589934841|#+---+----------+


