I am new to python and am struggling with this code. I have a csv file and am trying to create a function. The file, personal_info.csv , has a few columns with one labeled house_area and one named full_name. I am trying to create a code that will find the house with the largest area and return the name of person who owns it.
I am also not allowed to import anything besides the csv file, so I cannot use pandas.
Here's what some of the data looks like:
house_area | full_name |
---|---|
40.132 | John Smith |
85.832 | Anna Lee |
38.427 | Emma Jones |
So in this small sample I'm trying to find the house with the largest area (85.832) and print the person's name, Anna Lee. Except the actual file has a lot more rows
CodePudding user response:
Read and iterate over each row in the csv and check if the current value is bigger than the current biggest and update the value if its true:
import csv
with open("personal_info.csv", newline="") as csvfile:
reader = csv.DictReader(csvfile)
max_area = 0
max_name = ""
for row in reader:
area = float(row["house_area"])
if area > max_area:
max_area = area
max_name = row["full_name"]
print(max_name)
If there are two house which got the same size, than the name of the first one is returned.
CodePudding user response:
One simple way you can do this is by creating a variable called to track the largest house area. Let's call this largest_area
and set it to the value 0
(assuming all house areas in your CSV are greater than 0).
Then using, the csv library, go through each row in the CSV file, grab the house_area, and compare it to the largest_area
variable you created. If it is greater, update largest_area
, otherwise ignore and continue through the CSV file.
After you have finished going through the CSV file, the greatest area should be in your largest_area
variable.
CodePudding user response:
You can do that with csv by using csv.reader
and max
:
import csv
with open("personal_info.csv", "r") as f:
csv_reader = csv.reader(f, delimiter=",")
header = next(csv_reader)
max_row = max(csv_reader, key=lambda row: float(row[0]))
# Output
print(max_row)
['85.832', 'Anna Lee']