Q:

Cannot assign row in pandas.Dataframe

I am trying to calculate the mean of the rows of a DataFrame which have the same value on a specified column col. However I'm stuck at assigning a row of the pandas DataFrame.
Here's my code:
def code(data, col):
    """ Finds average value of all rows that have identical col values from column col .
        Returns new Pandas.DataFrame with the data
    """
    values = pd.unique(data[col])
    rows = len(values)
    res = pd.DataFrame(np.zeros(shape = (rows, len(data.columns))), columns = data.columns)
    for i, v in enumerate(values):
        e = data[data[col] == v].mean().to_frame().transpose()
        res[i:i+1] = e
    return res

The problem is that the code only works for the first row, and puts NaN values on the next rows. I have checked the value of e and confirmed it to be good, so there is a problem with the assignment res[i:i+1] = e. I have also tried to do res.iloc[i] = e but i get "ValueError: Incompatible indexer with Series" Is there an alternate way to do this? It seems very straight forward and I'm baffled why it doesn't work...
E.g:
wdata
    Out[78]: 
        Die  Subsite  Algorithm  Vt1           It1          Ignd
    0     1        0          0  0.0 -2.320000e-07 -4.862400e-08
    1     1        0          0  0.1 -1.000000e-04  1.000000e-04
    2     1        0          0  0.2 -1.000000e-03  1.000000e-03
    3     1        0          0  0.3 -1.000000e-02  1.000000e-02
    4     1        1          1  0.0  3.554000e-07 -2.012000e-07
    5     1        2          2  0.0  5.353000e-08 -1.684000e-07
    6     1        3          3  0.0  9.369400e-08 -2.121400e-08
    7     1        4          4  0.0  3.286200e-08 -2.093600e-08
    8     1        5          5  0.0  8.978600e-08 -3.262000e-07
    9     1        6          6  0.0  3.624800e-08 -2.507600e-08
    10    1        7          7  0.0  2.957000e-08 -1.993200e-08
    11    1        8          8  0.0  7.732600e-08 -3.773200e-08
    12    1        9          9  0.0  9.300000e-08 -3.521200e-08
    13    1       10         10  0.0  8.468000e-09 -6.990000e-09
    14    1       11         11  0.0  1.434200e-11 -1.200000e-11
    15    2        0          0  0.0  8.118000e-11 -5.254000e-11
    16    2        1          1  0.0  9.322000e-11 -1.359200e-10
    17    2        2          2  0.0  1.944000e-10 -2.409400e-10
    18    2        3          3  0.0  7.756000e-11 -8.556000e-11
    19    2        4          4  0.0  1.260000e-11 -8.618000e-12
    20    2        5          5  0.0  7.122000e-12 -1.402000e-13
    21    2        6          6  0.0  6.224000e-11 -2.760000e-11
    22    2        7          7  0.0  1.133400e-08 -6.566000e-09
    23    2        8          8  0.0  6.600000e-13 -1.808000e-11
    24    2        9          9  0.0  6.861000e-08 -4.063400e-08
    25    2       10         10  0.0  2.743800e-10 -1.336000e-10

Expected output:
     Die  Subsite  Algorithm   Vt1      It1     Ignd
0    1      4.4        4.4  0.04 -0.00074  0.00074
0    2      5.5        5.5    0  6.792247e-09 -4.023330e-09

Instead, what i get is:
         Die  Subsite  Algorithm   Vt1      It1     Ignd
    0    1      4.4        4.4  0.04 -0.00074  0.00074
    0    NaN      NaN      NaN  NaN   NaN      NaN      

For example, this code results in:
In[81]: wdata[wdata['Die'] == 2].mean().to_frame().transpose()
Out[81]: 
   Die  Subsite  Algorithm  Vt1           It1          Ignd
0    2      5.5        5.5    0  6.792247e-09 -4.023330e-09

A:

For me works:
def code(data, col):
    """ Finds average value of all rows that have identical col values from column col .
        Returns new Pandas.DataFrame with the data
    """
    values = pd.unique(data[col])
    rows = len(values)
    res = pd.DataFrame(columns = data.columns)
    for i, v in enumerate(values):
        e = data[data[col] == v].mean()
        res.loc[i,:] = e
    return res

col = 'Die'
print (code(data, col))
  Die Subsite Algorithm   Vt1          It1         Ignd
0   1     4.4       4.4  0.04 -0.000739957  0.000739939
1   2       5         5     0  7.34067e-09 -4.35482e-09

but same output has groupby with aggregate mean:
print (data.groupby(col, as_index=False).mean())
   Die  Subsite  Algorithm   Vt1           It1          Ignd
0    1      4.4        4.4  0.04 -7.399575e-04  7.399392e-04
1    2      5.0        5.0  0.00  7.340669e-09 -4.354818e-09

