I would like to get a final dataframe in which the tuple
'key'
is split into two columns,'hr'
and'filename'
, respectively.I also would like the output of the fit 'a, b, c'= *popt to be split into the three columns a, b, c.
In the current output dataframe, the last three columns do not contain the correct values. They show the initial a, b, c values, which are the initial guess of the fit. They should instead show the output of the fit ( *popt).
I attach my code, current wrong output, and correct output example. Thank you in advance
new_df = pd.DataFrame(columns=['hr', 'filename', 'a', 'b','c'])
new_df.columns = ['hr', 'filename', 'a', 'b','c']
################### curve fitting ########################################
grouped_df = HL.groupby(["hr", "filename"]) ## this is my initial dataframe
for key, g in grouped_df:
a = g['NPQ'].max()
b = g['NPQ'].min()
c = 0.36
popt, pcov = curve_fit(model, g['time'], g['NPQ'], p0 = np.array([a, b, c]), absolute_sigma=True)
print('Estimated parameters:
', popt))
##################### new data frame
new_row = {'hr': key, 'a':a, 'b':b, 'c':c }
new_df = new_df.append(new_row, ignore_index=True)
print(new_df)
An example of the correct output (I simplified it for efficiency):
hr filename a b c
8 20191129.0 21.22 0.55 0.45
8 20191129.0 .. .. ..
8 20191129.0 .. .. ..
14.0 20191129.0 .. .. ..
question from:https://stackoverflow.com/questions/65923080/how-to-split-a-tuple-into-multiple-columns-in-dictionary