Dayaan M. answered 08/25/26
Computer Science Graduate with Computer Engineering Coursework
In order to do this without Pandas, it helps to think about what Pandas is actually doing for you behind the scenes. It opens the file, splits every line into pieces, keeps track of which piece belongs to which column, turns those pieces from text into real numbers, and then averages them. Without Pandas we just do those same steps ourselves.
The one thing to know before we start is that when you read a file in Python, every line comes in as a string. So 34.2 in your file is the text "34.2" and not a number yet, which is usually where people get stuck, because you cannot add up strings.
Here is one way to do it:
total = 0
count = 0
with open("file.tsv") as f:
header = f.readline().strip().split("\t")
col = header.index("item_price")
for line in f:
parts = line.strip().split("\t")
total += float(parts[col])
count += 1
average = total / count
print(average)
Let me walk through what is happening. That first readline pulls off the header row by itself, and splitting it on the tab character gives us a list of the column names. Then header.index("item_price") tells us what position that column sits in. That is really the answer to your first question. You do not loop through the columns, you find the index of the one you want a single time and then use it on every row.
After that, the for loop goes through the remaining lines one at a time. For each line, split gives us a list of that row's values, parts[col] pulls out just the price, and float() turns that piece of text into a number we can actually work with. That is your second question, adding them up is just total += that number, and count goes up by one each time so we know how many rows we added.
So, our final answer is that the average is total divided by count, and if you ever want a different column you only have to change the name inside header.index.