首页 > 解决方案 > Spark获取2个特定行之间的行

问题描述

我有以下数据框: 在此处输入图像描述

我想获取值在 ('2/1/2020' 和 '2/5/2020') 之间的行 在此处输入图像描述

我试过了:

df.select([c for c in df.columns if c > '2/1/2020' & c < '2/5/2020']).show()

但我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
TypeError: unsupported operand type(s) for &: 'str' and 'str'

因为标题是 csv 类型(日期)谢谢!

标签: sqlscalaapache-sparkpysparkapache-spark-sql

解决方案


而不是&使用and布尔运算符。

df.select([c for c in df.columns if c > '2/1/2020' and c < '2/5/2020']).show()

Example:

df=spark.createDataFrame([(1,2,3,4,5,6)],['pro','2/1/2020','2/2/2020','2/3/2020','2/4/2020','2/5/2020'])

df.select([c for c in df.columns if c  > '2/1/2020' and c < '2/5/2020']).show()
#+--------+--------+--------+
#|2/2/2020|2/3/2020|2/4/2020|
#+--------+--------+--------+
#|       3|       4|       5|
#+--------+--------+--------+

推荐阅读