Skip to content

Scala to Python - pairRdd folder #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added pairRdd/aggregation/reducebykey/housePrice/*.py
  • Loading branch information
Pedro Bernardo committed Oct 2, 2017
commit fb3dfcbf7b00b5e7026c9b8aa8fd997d92d79d58
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pyspark import SparkContext

if __name__ == "__main__":

'''
Create a Spark program to read the house data from in/RealEstate.csv,
output the average price for houses with different number of bedrooms.

The houses dataset contains a collection of recent real estate listings in San Luis Obispo county and
around it. 

The dataset contains the following fields:
1. MLS: Multiple listing service number for the house (unique ID).
2. Location: city/town where the house is located. Most locations are in San Luis Obispo county and
northern Santa Barbara county (Santa Maria­Orcutt, Lompoc, Guadelupe, Los Alamos), but there
some out of area locations as well.
3. Price: the most recent listing price of the house (in dollars).
4. Bedrooms: number of bedrooms.
5. Bathrooms: number of bathrooms.
6. Size: size of the house in square feet.
7. Price/SQ.ft: price of the house per square foot.
8. Status: type of sale. Thee types are represented in the dataset: Short Sale, Foreclosure and Regular.

Each field is comma separated.

Sample output:

(3, 325000)
(1, 266356)
(2, 325000)
...

3, 1 and 2 mean the number of bedrooms. 325000 means the average price of houses with 3 bedrooms is 325000.

'''
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pyspark import SparkContext

if __name__ == "__main__":

sc = SparkContext("local", "avgHousePrice")
sc.setLogLevel("ERROR")

lines = sc.textFile("in/RealEstate.csv")
cleanedLines = lines.filter(lambda line: "Bedrooms" not in line)

housePricePairRdd = cleanedLines.map(lambda line: \
(line.split(",")[3], (1, float(line.split(",")[2]))))

housePriceTotal = housePricePairRdd \
.reduceByKey(lambda x, y: (x[0] + y[0], x[1] + y[1]))

print("housePriceTotal: ")
for bedroom, total in housePriceTotal.collect():
print("{} : {}".format(bedroom, total))

housePriceAvg = housePriceTotal.mapValues(lambda avgCount: avgCount[1] / avgCount[0])
print("\nhousePriceAvg: ")
for bedroom, avg in housePriceAvg.collect():
print("{} : {}".format(bedroom, avg))
7 changes: 7 additions & 0 deletions pairRdd/aggregation/reducebykey/housePrice/AvgCount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AvgCount():

def __init__(self, count: int, total: float):
self.count = count
self.total = total