-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.py
More file actions
24 lines (18 loc) · 1001 Bytes
/
Copy pathsum.py
File metadata and controls
24 lines (18 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, sum as spark_sum
# Step 1: Create a Spark session
spark = SparkSession.builder \
.appName("Wildlife Monitoring - Summing Species Count") \
.getOrCreate()
# Step 2: Load the cleaned data from the CSV file
file_path = 'dataset/output.csv/part-00000-f8f7d0bc-a5bc-40e3-a806-dc5dfb385e15-c000.csv' # Update this path to your cleaned data file
df = spark.read.csv(file_path, header=True, inferSchema=True)
# Step 3: Group by species and sum the individual counts
species_count = df.groupBy('species').agg(spark_sum('individualCount').alias('totalCount'))
# Step 4: Show the result (optional, for verification)
species_count.show(truncate=False)
# Step 5: Save the result to a new CSV file
output_path = 'dataset/species_total_count.csv' # Specify your output path
species_count.coalesce(1).write.csv(output_path, header=True, mode='overwrite')
# Step 6: Stop the Spark session
spark.stop()