熊猫:在多列之间线性插值多个步骤

时间:2020-02-26 14:45:09

标签: python pandas dataframe interpolation

我一直在这里查看问题的多个版本,但是找不到我要解决的问题的答案。

问题:
我有一个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间隔数据,以及一个带有0.1 nm间隔标题的空列TON。

我需要用插值数据填充所有这些空单元,插值数据是根据以2.5 nm间隔显示的数据计算得出的。

欢迎任何帮助,谢谢。

编辑1:这是我的输入数据框(data_in)和新的内插数据框(data_interp)的几张照片。

data_in

data_in

data_interp

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)

2 个答案:

答案 0 :(得分:0)

此解决方案有点难看,但应该可以解决问题:

##generate data
nrows = 100
cols = [x/10.0 for x in range(0, 100, 25)]
data = {c: np.random.uniform(0, 1, nrows) for c in cols}

df = pd.DataFrame(data)

enter image description here

 interpolation_steps = 25 
 dfs = []
 #Iterate on each interpolation pair(start, end)
 for col_ind in range(0, len(cols)-1):
     ##Using list comparison to iterate on each row, performin np.linspace on relevant columns values and creating a dataframe based on these results(along with column names).
     inter_df = pd.DataFrame([np.linspace(x, y, interpolation_steps) for x, y in 
     zip(df.iloc[:, col_ind], df.iloc[:, col_ind + 1])],
            columns=[i/10 for i in range(int(df.columns[col_ind]*10), 
            int(df.columns[col_ind+1]*10), 1)])
     dfs.append(inter_df)

  ##Merging interpolated dataframes back together into one big dataframe
  full_df = pd.concat(dfs, axis=1)
  ##adding last column because interpolated without it
  last_col = df.columns[-1]
  full_df.loc[:, last_col] = df[last_col]
  print(full_df.head(3).T)

enter image description here

答案 1 :(得分:0)

我将转置矩阵并欺骗(新)索引以使其成为DatetimeIndex-绝对值将相差1000倍,但对于数据并不重要。这样就可以只用不同的频率对数据帧进行重新采样。

在那之后,将索引转换回浮点并再次转置就足够了,以获得预期的结果。

从您的data_mini_dp开始,可能是:

df = data_mini_pd.T.set_index(pd.to_datetime(
    (data_mini_pd.columns * 10).astype(int), format='%f')
                              ).resample('100000ns').interpolate()

df.index = df.index.strftime('%f').astype('float64')/1000

resul = df.T

给予:

   400.0  400.1  400.2  400.3  400.4  400.5  400.6  400.7  400.8  400.9  401.0  401.1  401.2  401.3  401.4  401.5  401.6  401.7  401.8  401.9  402.0  402.1  402.2  402.3  402.4  402.5  402.6  402.7  402.8  402.9  403.0  403.1  403.2  403.3  403.4  403.5  403.6  403.7  403.8  403.9  404.0  404.1  404.2  404.3  404.4  404.5  404.6  404.7  404.8  404.9  405.0
0   10.0  10.12  10.24  10.36  10.48   10.6  10.72  10.84  10.96  11.08   11.2  11.32  11.44  11.56  11.68   11.8  11.92  12.04  12.16  12.28   12.4  12.52  12.64  12.76  12.88   13.0  12.92  12.84  12.76  12.68   12.6  12.52  12.44  12.36  12.28   12.2  12.12  12.04  11.96  11.88   11.8  11.72  11.64  11.56  11.48   11.4  11.32  11.24  11.16  11.08   11.0
1   15.0  14.96  14.92  14.88  14.84   14.8  14.76  14.72  14.68  14.64   14.6  14.56  14.52  14.48  14.44   14.4  14.36  14.32  14.28  14.24   14.2  14.16  14.12  14.08  14.04   14.0  14.04  14.08  14.12  14.16   14.2  14.24  14.28  14.32  14.36   14.4  14.44  14.48  14.52  14.56   14.6  14.64  14.68  14.72  14.76   14.8  14.84  14.88  14.92  14.96   15.0
2   19.0  18.96  18.92  18.88  18.84   18.8  18.76  18.72  18.68  18.64   18.6  18.56  18.52  18.48  18.44   18.4  18.36  18.32  18.28  18.24   18.2  18.16  18.12  18.08  18.04   18.0  18.16  18.32  18.48  18.64   18.8  18.96  19.12  19.28  19.44   19.6  19.76  19.92  20.08  20.24   20.4  20.56  20.72  20.88  21.04   21.2  21.36  21.52  21.68  21.84   22.0