python - Pandas 中的插值水平独立于每行

标签 python pandas resampling

我有一个像这样的数据框

ID,Time0,Sum0,Average0,Time1,Sum1,Average1
1,1520320347531.0,59.3635,18.2828,1520324772351.0,59.5031,18.4745
1,1519860442638.0,60.1159,20.3027,1519861181524.0,60.1033,20.31705

我想每 5 分钟水平插值一次。

如果数据垂直排列,此代码可以完美插值,但我不确定如何水平插值该行。即使两个不同的行中有相同的 Id,我也想让插值独立于每行。

df = df.set_index(['Time'])
df.index = pd.to_datetime(df.index, unit='ms')

df = (df.groupby('ID')[['Sum', 'Average']]
       .resample('5min')
       .mean()
       .groupby(level=0)
       .apply(lambda x: x.interpolate()).reset_index())

有什么建议吗?

最佳答案

我有一个答案,但它有点丑陋,对于任何看到过度操纵数据的人,请随时纠正它。

首先,根据您的数据,我将值 1520324772351.0(第一行,Time1 列)更改为 1520321086417.0,否则将超过 10 分钟,并使用以下示例到许多列。

根据您的数据,我首先创建一个串联数据框,例如

df_concat = (pd.concat([df[['ID','Time0','Sum0','Average0']]
                                .rename(columns={'Time0':'Time','Sum0':'Sum','Average0':'Average'}),
                       df[['ID','Time1','Sum1','Average1']]
                                .rename(columns={'Time1':'Time','Sum1':'Sum','Average1':'Average'})])
                    .sort_index())

获取这样的数据:

   ID          Time      Sum   Average
0   1  1.520320e+12  59.3635  18.28280
0   1  1.520321e+12  59.5031  18.47450
1   1  1.519860e+12  60.1159  20.30270
1   1  1.519861e+12  60.1033  20.31705

在这里,您可以使用您的方法处理索引列上带有 groupby 的列中的数据:

df_concat_set = df_concat.reset_index().set_index(['Time'])
df_concat_set.index = pd.to_datetime(df_concat_set.index, unit='ms')

df_concat_set = (df_concat_set.groupby('index')[['Sum', 'Average']]
                              .resample('5min')
                              .mean()
                              .groupby(level=0)
                              .apply(lambda x: x.interpolate())
                              .reset_index())

这里有数据:

   index                Time      Sum    Average
0      0 2018-03-06 07:10:00  59.3635  18.282800
1      0 2018-03-06 07:15:00  59.4333  18.378650
2      0 2018-03-06 07:20:00  59.5031  18.474500
3      1 2018-02-28 23:25:00  60.1159  20.302700
4      1 2018-02-28 23:30:00  60.1096  20.309875
5      1 2018-02-28 23:35:00  60.1033  20.317050

为了把它放回行,我这样做了(这里我确信有一个我不知道的pivot_table方法,但这种方式是有效的):

#first create a column with incremental number within a group of index:
df_concat_set['level_1'] = df_concat_set.groupby('index').cumcount()+1
# then set index and unstack
df_unstack = df_concat_set.set_index(['index','level_1']).unstack(level=1)
# here you have multiindex columns so change it to one level:
df_unstack.columns = [col[0]+str(col[1]-1) for col in df_unstack.columns]
# then change the order of columns (if necessary)
df_unstack = df_unstack[[ s+str(i) for i in range(len(df_unstack.columns)/3) 
                                        for s in ['Time','Sum','Average'] ]]

你的最终输出是:

                    Time0     Sum0  Average0               Time1     Sum1  \
index                                                                       
0     2018-03-06 07:10:00  59.3635   18.2828 2018-03-06 07:15:00  59.4333   
1     2018-02-28 23:25:00  60.1159   20.3027 2018-02-28 23:30:00  60.1096   

        Average1               Time2     Sum2  Average2  
index                                                    
0      18.378650 2018-03-06 07:20:00  59.5031  18.47450  
1      20.309875 2018-02-28 23:35:00  60.1033  20.31705 

这就是我希望你想要的。

正如我所说,这可能是过度操纵,但我找不到其他方法。

关于python - Pandas 中的插值水平独立于每行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50872972/

相关文章:

python - Pandas 在我的数据中按第一天重新采样

image-processing - 为什么我需要 lanczos(0) 的特例?

python - 跨包模块设置日志记录的有效方法

python - 排除具有空单元格的行

Python Pandas : differences between two dates in weeks?

python - Dask dataframe str.contains(regex=True) 不比 pandas 快

Python - 检查两个巨大的文本文件之间的一致性

python - 如何在标准环境中将文件加载到 Google-App-Engine

python - JSON到 Pandas 数据框

python - Pandas df.resample() : Specify NaN threshold for calculation of mean