- Extract the keys as
k1
and k2
instead of key
, because you do .groupby
on two columns.
- Then create
new_row
with 'hr'
as k1
, and 'filename'
as k2
- Instead of assigning the returned values to
popt
, you can assign them to (x, y, z)
.
for (k1, k2), g in df.groupby(["hr", "filename"]):
...
(x, y, z), pcov = curve_fit(model, g['time'], g['NPQ'], p0=np.array([a, b, c]), absolute_sigma=True)
...
new_row = {'hr': k1, 'filename': k2, 'a': x, 'b': y, 'c': z}
new_df = new_df.append(new_row, ignore_index=True)
- Alternatively,
key
is a tuple
because the .groupby
is on more than one column, so you can extract the separate values by calling the appropriate index.
- Create
new_row
with 'hr'
as key[0]
, and 'filename'
as key[1]
- If
popt
is a list
or tuple
, then you can assign the appropriate indices to 'a'
, 'b'
, and 'c'
.
for key, g in df.groupby(["hr", "filename"]):
...
popt, pcov = curve_fit(model, g['time'], g['NPQ'], p0 = np.array([a, b, c]), absolute_sigma=True)
...
new_row = {'hr': key[0], 'filename': key[1], 'a': popt[0], 'b': popt[1], 'c': popt[2]}
new_df = new_df.append(new_row, ignore_index=True)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…