简体   繁体   中英

Finding average of every column from CSV file using Python?

I have a CSV file, which has several columns and several rows. Please, see the picture above. In the picture is shown just the two first baskets, but in the original CSV -file I have hundreds of them. [1]: https://i.stack.imgur.com/R2ZTo.png

I would like to calculate average for every Fruit in every Basket using Python. Here is my code but it doesn't seem to work as it should be. Better ideas? I have tried to fix this also importing and using numpy but I didn't succeed with it.

I would appreciate any help or suggestions. I'm totally new in this.

import csv
from operator import itemgetter


fileLineList = []
averageFruitsDict = {} # Creating an empty dictionary here.

with open('Fruits.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        fileLineList.append(row)

for column in fileLineList:
    highest = 0
    lowest = 0
    total = 0
    average = 0
    for column in row:
        if column.isdigit():
            column = int(column)
            if column > highest:
                highest = column
            if column < lowest or lowest == 0:
                lowest = column
            total += column    
    average = total / 3
  
    averageFruitsDict[row[0]] = [highest, lowest, round(average)]

averageFruitsList = []


for key, value in averageFruitsDict.items():
    averageFruitsList.append([key, value[2]])


print('\nFruits in Baskets\n')
print(averageFruitsList)

--- So I'm know trying with this code:

import pandas as pd

fruits = pd.read_csv('fruits.csv', sep=';')
print(list(fruits.columns))
fruits['Unnamed: 0'].fillna(method='ffill', inplace = True)
fruits.groupby('Unnamed: 0').mean()
fruits.groupby('Bananas').mean()
fruits.groupby('Apples').mean()
fruits.groupby('Oranges').mean()
fruits.to_csv('results.csv', index=False)

It creates a new CSV file for me and it looks correct, I don't get any errors but I can't make it calculate the mean of every fruit for every basket. Thankful of all help!

So using the image you posted and replicating/creating an identical test csv called fruit - I was able to create this quick solution using pandas.

import pandas as pd
fruit = pd.read_csv('fruit.csv')

在此处输入图像描述

With the unnamed column containing the basket numbers with NaNs in between - we fill with the preceding value. By doing so we are then able to group by the basket number (by using the 'Unnamed: 0' column and apply the mean to all other columns)

fruit['Unnamed: 0'].fillna(method='ffill', inplace = True)

fruit.groupby('Unnamed: 0').mean()

This gets you your desired output of a fruit average for each basket (please note I made up values for basket 3)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM