0

There are multiple posts about creating dictionaries with multiple values per key but I am missing what I am doing wrong.

I want to create a dictionary with selected col headers from a dataframe as keys. There should be two values:

value1 = mean of first ten values in the column value2 = mean of last ten values in the column

    band_list = ['A', 'B', 'D', 'E', 'F']
    def mean_ten(df, band_list):
       mean_dict = {}
       for wave in band_list:
            col = df.columns.get_loc(wave);print(col)
            mean_dict[wave] = df.iloc[:9, col].mean(axis=0)
            mean_dict[wave].append(df.iloc[-10:,col].mean(axis = 0))
    #Convert dictionary to df and return
cs95
  • 379,657
  • 97
  • 704
  • 746
Brain_overflowed
  • 426
  • 1
  • 5
  • 21
  • Do you mean to do: `mean_dict[wave] = [df.iloc[:9, col].mean(axis=0)]` - initialising a single element list so you can append to it? – cs95 Oct 04 '17 at 21:24
  • i was trying to add a value to a key in the dictionary. I used the append looking at @coldspeed – Brain_overflowed Oct 04 '17 at 21:35
  • That is incorrect. My previous comment already shows the fix (besides that line, everything else is the same), so you'd best be implementing it and seeing if it works after that. – cs95 Oct 04 '17 at 21:39
  • My output: NONE =/ – Brain_overflowed Oct 04 '17 at 21:42
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – cs95 Oct 04 '17 at 21:42

0 Answers0