spark获取两个特定行之间的行

wnavrhmk  于 2021-05-29  发布在  Spark
关注(0)|答案(1)|浏览(498)

我有以下Dataframe:

我要获取值介于('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类型(日期)谢谢!

nwlqm0z1

nwlqm0z11#

而不是 & 使用 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|

# +--------+--------+--------+

相关问题