Want to calculate average from comma separated text files [closed]

Asked 2 years ago, Updated 2 years ago, 370 views

Do you want to improve this question?Edit your post to clarify the issue you are trying to resolve by adding details.

Closed to 3 months ago

Three months ago

Example

Weather.txt has monthly average data for Ibaraki from January 2000 to December 2021.
It is stored in order of year, January temperature, February temperature, …, December temperature.

Program to read weather.txt and output average temperature for each year and month

 2000, 7.6, 6.0, 9.4, 14.5, 19.8, 22.5, 27.7, 28.3, 25.6, 18.8, 13.3, 8.8
2001,4.9,6.6,9.8,15.7,19.5,23.1,28.5,26.4,23.2,18.7,13.1,8.4
2002,7.4,7.9,12.2,16.1,18.4,21.6,28.0,28.0,23.1,19.0,11.6,7.2
2003,5.5,6.4,8.7,15.1,18.8,23.2,22.8,26.0,24.2,17.8,14.4,9.2
2004,6.3,8.5,9.8,16.4,19.6,23.7,28.5,27.2,25.1,17.5,15.6,9.9
2005,6.1,6.2,9.0,15.1,17.7,23.2,25.6,28.1,24.7,19.2,13.3,6.4
2006,5.1,6.7,9.8,13.6,19.0,22.5,25.6,27.5,23.5,19.5,14.4,9.5
2007,7.6,8.6,10.8,13.7,19.8,23.2,24.4,29.0,25.2,19.0,13.3,9.0
2008,5.9,5.5,10.7,14.7,18.5,21.3,27.0,26.8,24.4,19.4,13.1,9.8
2009,6.8,7.8,10.0,15.7,20.1,22.5,26.3,26.6,23.0,19.0,13.5,9.0
2010,7.0,6.5,9.1,12.4,19.0,23.6,28.0,29.6,25.1,18.9,13.5,9.9
2011,5.1,7.0,8.1,14.5,18.5,22.8,27.3,27.5,25.1,19.5,14.9,7.5
2012,4.8,5.4,8.8,14.5,19.6,21.4,26.4,29.1,26.2,19.4,12.7,7.3
2013,5.5,6.2,12.1,15.2,19.8,22.9,27.3,29.2,25.2,19.8,13.5,8.3
2014,6.3,5.9,10.4,15.0,20.3,23.4,26.8,27.7,23.2,19.1,14.2,6.7
2015,5.8,5.7,10.3,14.5,21.1,22.1,26.2,26.7,22.6,18.4,13.9,9.3
2016,6.1,7.2,10.1,15.4,20.2,22.4,25.4,27.1,24.4,18.7,11.4,8.9
2017,5.8,6.9,8.5,14.7,20.0,22.0,27.3,26.4,22.8,16.8,11.9,6.6
2018,4.7,5.4,11.5,17.0,19.8,22.4,28.3,28.1,22.9,19.1,14.0,8.3
2019,5.6,7.2,10.6,13.6,20.0,21.8,24.1,28.4,25.1,19.4,13.1,8.5
2020,7.1,8.3,10.7,12.8,19.5,23.2,24.3,29.1,24.2,17.5,14.0,7.7
2021,5.4,8.5,12.8,15.1,19.6,22.7,25.9,27.4,22.3,18.2,13.7,7.9

Please tell me what kind of program it will be

python

2022-09-30 22:04

2 Answers

When asked, "What are you having trouble with?" I guess you can only say, "I don't understand everything."If that's the case, the range of challenges is too wide.First, break down what you do into small steps and work on them one by one.Smaller steps are easy to search by keyword or use on these questioning sites.

Read data line by line

with open("weather.txt") as file:
    For line in file:
        line=line.rstrip()#Delete line feed

Note:

Separate lines with delimiters

str_list=line.split(",")
year=str_list[0]

Note: str.split

Convert string to numeric (floating point)

 for month in range (1,13):
    temperature=float(str_list[month])

Note:

To sum up so far, for example,

with open("weather.txt") as file:
    For line in file:
        line=line.rstrip()#Delete line feed
        str_list = line.split(", ")
        year=str_list[0]
        print(f"--{year} year--")

        for month in range (1,13):
            temperature=float(str_list[month])
            print(f"{year} year {month} month temperature: {temperature}")

In some cases, the data is stored in a list instead of being used immediately.I'm going to do it this time because it's simple.

Find the mean

The average is

  • Preparing variables with names such as ~total and initializing them with 0
  • Adding element numbers such as
  • +=
  • When you're done adding everything, divide by the number of elements

and so on."In the case of the question, ""average of each month"" may be a little difficult."From January to December, it is not good to have 12 variables, such as month1_total, month2_total.In these cases, put them together in a list.

# Example:

# initialization
month_total_list = [0.0]*12

# addition
month_total_list[i]+=temperature

Note that the index of the list starts with 0, which is one off the name of the month.Insert these averaging codes into your previous code and you'll be done.

with open("weather.txt") as file:
    month_total_list = [0.0]*12
    year_count = 0

    For line in file:
        year_count+=1

        line=line.rstrip()#Delete line feed
        str_list = line.split(", ")
        year=str_list[0]
        # print(f"--{year} --")

        year_total = 0.0
        for month in range (1,13):
            temperature=float(str_list[month])
            # print(f"{year} year {month} month temperature: {temperature}")
            year_total+=temperature
            month_total_list [month-1] + = temperature
        print(f"{year} year average temperature: {year_total/12}")

    print("--average for each month--")
    for i in range (0,12):
        print(f"{i+1} month average temperature: {month_total_list[i]/year_count}")

The above code has not been tested.In addition, Floating-point calculations have errors.


2022-09-30 22:04

If you ignore learning situations that are probably in the early stages, using pandas is short and easy.
These four lines allow you to read and calculate the average value.

import pandas as pd
df=pd.read_csv('weather.txt', header=None, index_col=0)
df ['Average annual temperature'] = df.mean(axis=1)
df = pd.concat ([df, pd.DataFrame(df.mean(), columns=['Monthly Average Temperature'].T], axis=0)

You can continue to see everything, including the original data.

pd.set_option('display.unicode.east_asian_width', True)
pd.options.display.float_format='{:,.1f}'.format
print(df)

If you only want to see the calculated value, do the following instead of print(df) above:

 print ('average annual temperature')
print(df['average annual temperature'][:-1].to_string())
print('')
print(df.iloc [-1:, 0:12].T)


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.