【发布时间】:2020-02-26 14:45:09
【问题描述】:
我一直在这里查看我的问题的多个版本,但找不到我正在尝试做的事情的答案。
问题:
我有一个带有一堆十进制数字数据的 Pandas 数据框,这些数据是通过实验的多次迭代(每行)收集的,用于多种波长的光(每列)。这些波长间隔是列标题,由于我们机器的限制,波长/列之间的间隔目前是2.5。
我现在需要计算波长间隔为 0.1 而不是 2.5 时每行的值应为多少。这将要求我创建间距为 0.1 的新列标题(因此我当前的每个列之间有 24 个新列),然后在每个 0.1 步长处对每行中的值进行线性插值。
有人可以帮忙吗?我完全不知道如何做到这一点。
到目前为止我的收获:
# data_in = my original Panda dataframe with experiment data.
# wavelengths (column headers) go from 400 to 900 in 2.5nm intervals.
# I want 400 to 900 in 0.1 nm intervals.
# Create a copy dataframe for generating the interpolated columns,
# copying the structure of the original file for the first 3 columns.
# (I need the first 3 columns intact for an unimportant reason)
data_interp = data_in[data_in.columns[0:3]].copy()
# Interpolate 400 to 900 nm in 0.1 nm steps for the column headers.
wave_array = np.linspace(400, 900, num=5000, endpoint=True)
# Import the interpolated numpy array as column headers in the new panda dataframe.
data_interp = pd.concat([data_interp,pd.DataFrame(columns=wave_array)])
# Use the pandas 'update' function to map any matching instances of columns and their data
# from 'data_in' to 'data_interp' (ie, import all the 2.5 nm interval data from
# the old dataframe to their proper place in the new dataframe).
data_interp.update(data_in)
现在我有一个新的 Panda 数据帧 (data_interp),其中包含我所有的原始 2.5 nm 间隔数据,以及 TON 的空列和 0.1 nm 间隔标题。
我需要用插值数据填充所有这些空单元格,这些插值数据是根据以 2.5 nm 间隔存在的数据计算得出的。
欢迎任何帮助,谢谢。
编辑 1:这是我的输入数据帧 (data_in) 和我的新插值数据帧 (data_interp) 的几张照片。
data_in:
data_interp:
编辑 2:小型化示例。
# Mini data.
data_mini = [[10, 13, 11], [15, 14, 15], [19, 18, 22]]
# Convert to pandas dataframe
data_mini_pd = pd.DataFrame(data_mini, columns = [400, 402.5, 405])
# Copy new dataframe based on original dataframe
data_mini_pd_interp = data_mini_pd[data_mini_pd.columns[0:0]].copy()
# Interpolate 400 to 405 nm in 0.1 nm steps for the column headers.
wave_array_mini = np.linspace(400, 405, num=50, endpoint=True)
# Round all numbers to 1 decimal place, to prevent float placeholder overflow
# when importing to panda column headers.
wave_array_mini_round = np.around(wave_array_mini, decimals=1)
# Import the interpolated numpy array as column headers in the new panda dataframe.
data_mini_pd_interp = pd.concat([data_mini_pd_interp,pd.DataFrame(columns=wave_array_mini_round)])
# Use the pandas 'update' function to map any matching instances of columns and their data from 'data_in' to 'data_interp' (ie, import all the 2.5 nm interval
# data from the old dataframe to their proper place in the new dataframe).
data_mini_pd_interp.update(data_mini_pd)
【问题讨论】:
-
您能否向我们提供一些示例数据(几行原始数据)并展示您目前得到的数据?
-
@SergeBallesta 我添加了指向我的两个数据帧的屏幕截图的链接。
-
我们无法从图像中复制任何内容...您应该提供可复制的数据。顺便说一句,为什么数据框 column wise?使用 DateTimeIndex 标记行更为常见。
-
@SergeBallesta 好的,我已经包含了一个我正在使用的数据的迷你示例。我无法控制传入数据的格式——这就是我获取它的方式。正如您所说,数据由标记行的 DateTime 索引组织(我在显示期间切断了这些列,因为它包含一些敏感信息)。
标签: python pandas dataframe interpolation