Generation

generate functionMon, 06 Feb 2023

There is a df Pandas dataframe: date av_temp deviations country year decade 577457 2013-05-01 19.059 1.022 Zimbabwe 2013 2010-2020 577458 2013-06-01 17.613 0.473 Zimbabwe 2013 2010-2020 577459 2013-07-01 17.000 0.453 Zimbabwe 2013 2010-2020 577460 2013-08-01 19.759 0.717 Zimbabwe 2013 2010-2020 Write a function that: 1) leave only the rows in the year column with a value higher than 1980 2) calculates the average temperature of the countries (the average of the av_temp column) 3) builds a list of the 20 coldest countries in ascending av_temp order

def filter_1980(df): df["year"] = df["year"].apply(lambda x: x > 1980) return df def avg_temp(df): return df["av_temp"].mean() def list_coldest(df, n=20): df = df.sort_values(by="av_temp") return list(df.sample(n)["country"]) new_df = filter_1980(df) avg_temp(new_df) list_coldest(new_df)

Questions about programming?Chat with your personal AI assistant