Skip to content Skip to sidebar Skip to footer

Python Curve Fitting On Pandas Dataframe Then Add Coef To New Columns

I have a dataframe that needs to be curve fitted per row (second order polynomial). There are four columns, each column name denotes the x value. Each row contains 4 y values corre

Solution 1:

I found the following numpy.polynomial.polynomial.polyfit which is an alternative to np.polyfit that takes a 2-D array for y.

Starting your code from x, I get the following:

my_coef_array = pd.DataFrame(np.polynomial.polynomial.polyfit(x, pivot_df.T.values, 2)).T
my_coef_array.index = pivot_df.index
my_coef_array.columns = ['c', 'm1', 'm2']

pivot_df = pivot_df.join(my_coef_array)

Post a Comment for "Python Curve Fitting On Pandas Dataframe Then Add Coef To New Columns"