2016-04-22 96 views
2

过滤火花数据框我创建了一个具有以下模式的数据帧:如何通过一个布尔列

In [43]: yelp_df.printSchema() 
root 
|-- business_id: string (nullable = true) 
|-- cool: integer (nullable = true) 
|-- date: string (nullable = true) 
|-- funny: integer (nullable = true) 
|-- id: string (nullable = true) 
|-- stars: integer (nullable = true) 
|-- text: string (nullable = true) 
|-- type: string (nullable = true) 
|-- useful: integer (nullable = true) 
|-- user_id: string (nullable = true) 
|-- name: string (nullable = true) 
|-- full_address: string (nullable = true) 
|-- latitude: double (nullable = true) 
|-- longitude: double (nullable = true) 
|-- neighborhoods: string (nullable = true) 
|-- open: boolean (nullable = true) 
|-- review_count: integer (nullable = true) 
|-- state: string (nullable = true) 

现在我只需要选择“公开”一栏是“真”的记录。如下所示,它们中有很多是“开放的”。

business_id   cool date  funny id     stars text     type  useful user_id    name    full_address   latitude  longitude  neighborhoods open review_count state 
9yKzy9PApeiPPOUJE... 2 2011-01-26 0  fWKvX83p0-ka4JS3d... 4  My wife took me h... business 5  rLtl8ZkDX5vH5nAx9... Morning Glory Cafe 6106 S 32nd St Ph... 33.3907928467 -112.012504578 []   true 116   AZ 
ZRJwVLyzEJq1VAihD... 0 2011-07-27 0  IjZ33sJrzXqU-0X6U... 4  I have no idea wh... business 0  0a2KyEL0d3Yb1V6ai... Spinato's Pizzeria 4848 E Chandler B... 33.305606842 -111.978759766 []   true 102   AZ 
6oRAC4uyJCsJl1X0W... 0 2012-06-14 0  IESLBzqUCLdSzSqm0... 4  love the gyro pla... business 1  0hT2KtfLiobPvh6cD... Haji-Baba   1513 E Apache Bl... 33.4143447876 -111.913032532 []   true 265   AZ 
_1QQZuf4zZOyFCvXc... 1 2010-05-27 0  G-WvGaISbqqaMHlNn... 4  Rosie, Dakota, an... business 2  uZetl9T0NcROGOyFf... Chaparral Dog Park 5401 N Hayden Rd ... 33.5229454041 -111.90788269 []   true 88   AZ 
6ozycU1RpktNG2-1B... 0 2012-01-05 0  1uJFq2r5QfJG_6ExM... 4  General Manager S... business 0  vYmM4KTsC8ZfQBg-j... Discount Tire  1357 S Power Road... 33.3910255432 -111.68447876 []   true 5   AZ 

但是下面的命令我在pyspark运行没有返回值:

yelp_df.filter(yelp_df["open"] == "true").collect() 

什么是做正确的方式?

回答

2

您正在比较数据类型不正确。 open被列为布尔值,而不是字符串,因此yelp_df["open"] == "true"不正确 - "true"是一个字符串。

相反,你想要做

yelp_df.filter(yelp_df["open"] == True).collect() 

open值这个比较正确对基本布尔True,而非非布尔字符串"true"