我有一个这样的数据帧,
数据帧说明
我希望将名为"地平线方位角"的列插值为 1 的 (0,1,2,3,....(,并相应地线性插值名为"地平线高度"的列。
我不太确定,该怎么做。我看到的大多数数据帧插值是关于 NaN 值填充的。
谢谢 德巴扬
将Horizon Azimuth
列设为索引,然后使用 pd。DataFrame.reindex 以添加所需的索引。这将导致Horizon Height
列在新创建的行中具有 NaNs 值。然后使用熊猫。DataFrame.interpolate,将NaN替换为线性插值。
import pandas as pd
df = pd.DataFrame({'Horizon Azimuth': [2,5,7,8], 'Horizon Height': [0,4,6,11]})
df = df.set_index('Horizon Azimuth')
new_index = range(2,9) # [2, 3, 4, 5, 6, 7, 8]
df = df.reindex(new_index)
df = df.interpolate(method='linear')
df.reset_index(inplace=True)
print(df)
输出:
Horizon Azimuth Horizon Height
0 2 0.000000
1 3 1.333333
2 4 2.666667
3 5 4.000000
4 6 5.000000
5 7 6.000000
6 8 11.000000
希望这有帮助。