Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 how can I write my function output on CSV file
#11
A way that is much faster is:
import pandas as pd

df = pd.read_csv("grades.csv")
grades = df.iloc[:, 1:]
names = df.iloc[:, 0]
averages = grades.mean(axis=1)

output = pd.DataFrame(names, averages)
output.to_csv("Final_grades.csv", index=False)
buran wrote May-02-2019, 04:50 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Quote
#12
@rxndy, this is most probably just a school project/homework assignment and I doubt they will be allowed to to use pandas
and please fix your code because you will get NameError on line 6 - now it is fixed
Quote
#13
(May-02-2019, 04:52 PM)buran Wrote: @rxndy, this is most probably just a school project/homework assignment and I doubt they will be allowed to to use pandas
and please fix your code because you will get NameError on line 6


True, My bad. I probably just confused people even more.
Quote
#14
I have another question.

can I use function and mixed a number of operation on one csv file?

for the same code:

import csv
from statistics import mean
import itertools
from collections import OrderedDict

def calculate_averages(input_file_name, output_file_name):
    with open ('grades.csv', 'r') as input_file_name:
        reader=csv.reader(input_file_name)
        data = []
        for row in reader:
            l2 = []
            name = row[0]
            grades=[int(num) for num in row[1:]] 
            l2.append(name)
            l2.append(float(mean(grades)))
            data.append(l2)
            
    with open('grades.csv', 'w', newline='') as output_file_name:
        writer = csv.writer(output_file_name)
        writer.writerows(data)
and after that i am going to use another function...
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 6 145 Oct-18-2019, 11:44 PM
Last Post: snippsat
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 108 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  How do I write class objects to a file in binary mode? Exsul1 7 399 Sep-14-2019, 09:33 PM
Last Post: snippsat
  how do i write a cross-platform relative file path? pseudo 2 124 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  write to txt file one line antmar904 3 240 Aug-16-2019, 02:40 PM
Last Post: Axel_Erfurt
  python file output to log file Rsh 4 303 Aug-13-2019, 09:00 PM
Last Post: DeaD_EyE
  Not quite getting the correct Output from a function twinpiques 3 220 Aug-04-2019, 11:53 PM
Last Post: twinpiques
  read text file and write into html with correct link jacklee26 4 239 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  using output of one function into another function ibaad1406 7 327 Jun-24-2019, 08:43 AM
Last Post: noisefloor
  is it possible to use python to read and write from a jsx file? nadavrock 3 355 Jun-08-2019, 02:59 AM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)