Теперь я использовал более общий способ интерполяции данных к определенному индексу.Я просто хочу перечислить свой подход для будущих ссылок:
import numpy as np
import pandas as pd
from scipy.interpolate import interp1d
# Example data 5 numeric columns
i = pd.RangeIndex(0, 430, 1)
df1 = pd.DataFrame([-0.47, -0.12, 0.55, 0.72, 1.8, 1.1, 0.43, -0.29,
-0.55, -0.6, -0.32, 0.28, 0.72, 1.1 , 1.34, 1.32,
1.11, 0.46, 0.09, 0.02], [-0.47, -0.12, 0.55, 0.72, 1.8, 1.1, 0.43, -0.29,
-0.55, -0.6, -0.32, 0.28, 0.72, 1.1 , 1.34, 1.32,
1.11, 0.46, 0.09, 0.02], [-0.47, -0.12, 0.55, 0.72, 1.8, 1.1, 0.43, -0.29,
-0.55, -0.6, -0.32, 0.28, 0.72, 1.1 , 1.34, 1.32,
1.11, 0.46, 0.09, 0.02])
# Select numeric columns
nums = df1.select_dtypes([np.number])
old_idx = df.index
# Calculate new index
len_idx = env.shape[0]
mi, ma = old_idx.min(), old_idx.max()
new_idx = np.linspace(mi, ma, len_idx)
# Plot to compare interpolation to original values
fig, ax = plt.subplots(1, 1)
ax.plot(old_idx, df1.iloc[:, 0], 'k--')
def interpol(column):
```Interpolation function```
interpolant = interp1d(old_idx, column)
interpolated = interpolant(new_idx)
return interpolated
# Interpolate data to match index length of enviromental data
inter_nums = pd.DataFrame(index=new_idx)
for col in nums:
inter = interpol(nums[col])
inter_nums[col] = inter
# Plot after interpolation. Same curve? good!
ax.plot(inter_nums_iloc[:; 0], c='r')