Python Repeated Output

Asked 1 years ago, Updated 1 years ago, 295 views

for year in range(1943,1945):
    for month in range(1, 13):
        # Calculate the last day of each month
        if month in [1, 3, 5, 7, 8, 10, 12]:
            last_day = 31
        elif month == 2:
            last_day = 29      
        else:
            last_day = 30

        # Calculate the average value by filtering by changing the date value by month
        for day in range(1, last_day+1):
            filtered_df = df[(df['Month'] == month) & (df['Day'] == day)]
            filter_df = df[(yeosu_data['Year'] == year) & (df['Month'] == month) & (df['Day'] == day)]
            final_temperature = filter_df['Temperature'].tolist()
            avg_temperature = filtered_df['Temperature'].mean()

            # Save Results to List

            result_list.append({'월': month, '일': day, '기온(K) 평균': final_temperature})

result_list 

If you want the temperature average to be an integer, but final_temperature=filter_df['Temperature'].tolist()[0] will result in the following error.

list index out of range

How can I resolve the error?

python

2023-03-31 14:42

1 Answers

On February 29, 1943, filter_df has zero row.


2023-04-01 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.