如何用函数 (sin,line) 填充 pandas 列?

How can I fill pandas column with a function (sin,line)?

我有一个数据框,其中包含我自己添加的一些列。有一个特定的列收集最大和最小潮位。

Pandas Column mostly empty but with some reference values

import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,2,3,4],'b':[np.nan,np.nan,3,4]},columns=['a','b']) 
df

问题在于该列大部分是空的,因为它只显示那些峰值而不显示中间值。我想用类似于下图所示的函数来填充缺失值。

I want to fill it with a function of this kind

提前致谢。

由于您没有指定 pandas 数据框使用的日期时间格式,这里是一个包含索引数据的示例。如果它们间隔均匀且没有间隙,您可以使用它们。

import pandas as pd
import numpy as np
from scipy.optimize import curve_fit

tide = np.asarray([-1.2,np.nan,np.nan,3.4,np.nan,np.nan,-1.6,np.nan,np.nan,3.7,np.nan,np.nan,-1.4,])
tide_time = np.arange(len(tide))
df = pd.DataFrame({'a':tide_time,'b':tide}) 

#define your fit function with amplitude, frequence, phase and offset
def fit_func(x, ampl, freq, phase, offset):
    return ampl * np.sin(freq * x + phase) + offset

#extract rows that contain your values
df_nona = df.dropna()

#perform the least square fit, get the coefficients for your fitted data
coeff, _mat = curve_fit(fit_func, df_nona["a"], df_nona["b"])
print(coeff)

#append a column with fit data
df["fitted_b"] = fit_func(df["a"], *coeff)

我的示例数据的输出

#amplitude    frequency   phase       offset
[ 2.63098177  1.12805625 -2.17037976  1.0127173 ]

     a    b  fitted_b
0    0 -1.2 -1.159344
1    1  NaN -1.259341
2    2  NaN  1.238002
3    3  3.4  3.477807
4    4  NaN  2.899605
5    5  NaN  0.164376
6    6 -1.6 -1.601058
7    7  NaN -0.378513
8    8  NaN  2.434439
9    9  3.7  3.622127
10  10  NaN  1.826826
11  11  NaN -0.899136
12  12 -1.4 -1.439532