Write a Python function rainaverage(l)Â
Write a Python function rainaverage(l) that takes as input a list of rainfall recordings and computes the avarage rainfall for each city. The output should be a list of pairs (c,ar)where c is the city and ar is the average rainfall for this city among the recordings in the input list. Note that arshould be of type float. The output should be sorted in dictionary order with respect to the city name. Here are some examples to show how rainaverage(l) should work. >>> rainaverage([(1,2),(1,3),(2,3),(1,1),(3,8)]) [(1, 2.0), (2, 3.0), (3, 8.0)] >>> rainaverage([('Bombay',848),('Madras',103),('Bombay',923),('Bangalore',201),('Madras',128)]) [('Bangalore', 201.0), ('Bombay', 885.5), ('Madras', 115.5)] I tried this, but it didn't work. I'm struck up import pandas as pd def l = [('Bombay',848),('Madras',103),('Bombay',923),('Bangalore',201),('Madras',128)] df = pd.DataFrame(l, columns=['city', 'rainfall']) output = [] for c in df['city'].unique(): output += [(c, df[df['city'] == c]['rainfall'].mean())] print(output)