python - python - interpolation in pandas
问题描述
this is a follow up from my question here
python - interpolation on dataframe values
import pandas as pd
import numpy as np
from scipy.interpolate import interp1d
def interpolate_iv_my(x,y,newX_Value):
y_interp = interp1d(x,y)
iv = y_interp(newX_Value)
return iv
import pandas as pd
df = pd.DataFrame({'30': [-23, 12, -12, 10, -23, 12, -32, 15, -20, 10],
'40': [-30, 20, -21, 15, -33, 22, -40, 25, -22, 12],
'50': [-40, 25, -26, 19, -39, 32, -45, 35, -32, 18],
'60': [-45, 34, -29, 25, -53, 67, -55, 45, -42, 19],
})
x = [30,40,50,60]
df['x_'] = np.random.choice([35,33,42,52],10).tolist()
cols = ['30','40','50','60']
df['new'] = df[cols].to_numpy().tolist()
df['interpolate2'] = df['new'].apply(lambda y: interpolate_iv_my(x,y, df['x_']))
the problem with the code is in this line
df['interpolate2'] = df['new'].apply(lambda y: interpolate_iv_my(x,y, df['x_']))
df['x_'] sends the entire column instead just one value on that row for df['x_']. How do I fix that?
解决方案
使用DataFrame.apply
而不是Series.apply
:
df['interpolate2'] = \
df.apply(lambda row: interpolate_iv_my(x, row['new'], row['x_']), axis=1)
print(df)
# Output:
30 40 50 60 x_ new interpolate2
0 -23 -30 -40 -45 35 [-23, -30, -40, -45] -26.5
1 12 20 25 34 52 [12, 20, 25, 34] 26.8
2 -12 -21 -26 -29 33 [-12, -21, -26, -29] -14.7
3 10 15 19 25 33 [10, 15, 19, 25] 11.5
4 -23 -33 -39 -53 52 [-23, -33, -39, -53] -41.8
5 12 22 32 67 42 [12, 22, 32, 67] 24.0
6 -32 -40 -45 -55 35 [-32, -40, -45, -55] -36.0
7 15 25 35 45 35 [15, 25, 35, 45] 20.0
8 -20 -22 -32 -42 42 [-20, -22, -32, -42] -24.0
9 10 12 18 19 42 [10, 12, 18, 19] 13.2
推荐阅读
- assembly - 在汇编代码中指定源代码中的 ASCII 字符的三种语法是什么?
- c++ - cannot Segment a RGB image by pointer based accessing pixel intensities
- javascript - Angular 2+ 在 app.component.ts 方法中使用 jQuery 的附加选项
- python-3.x - 理解排序函数python的工作原理
- git - Windows 上的 Git text=auto core.autocrlf=false 提供 CRLF
- c++ - 使用 GDB 并检查 Data 的内存布局
- c# - 如何在返回类型为 Task 的方法中尝试捕获?
- python - python生成器比嵌套for循环快吗?
- sql-server - SQL Server:仅选择具有多列的记录的第一个实例
- java - 多个面板 - 如何将一个按钮从一个类链接到另一个类?