我有两个数据集:随时间变化的温度A和随时间变化的温度B。它们都在相同的时间间隔内。问题在于,一组具有375,638个值,另一组具有66,933个值。 A和B中每个数据点之间的时间从5秒到60秒不等。
我想通过获取每秒或平均值(可能是线性插值)的平均值,将数据集A缩减为与B相同的大小,但是我不确定该怎么做。我正在编写的函数变得非常复杂,并且与多个嵌套循环相混淆,因此我想知道是否有一些简单的方法可以使我忽略。
谢谢
答案 0 :(得分:0)
您可以使用numpy中的 split()函数:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(20, 4), columns=list('abcd'))
df1, df2 = np.split(df, [int(len(df)/2)], axis=0)
输出df
a b c d
0 0.175034 0.491546 0.214332 0.551543
1 0.360353 0.529040 0.949357 0.990295
2 0.475146 0.684860 0.252388 0.319754
3 0.296353 0.668867 0.806810 0.303479
4 0.125018 0.195096 0.470490 0.535244
5 0.279616 0.888702 0.925141 0.142643
6 0.713421 0.762474 0.332148 0.546850
7 0.908884 0.247675 0.008861 0.291648
8 0.002747 0.513682 0.609479 0.215230
9 0.789802 0.270530 0.390524 0.356701
10 0.621201 0.502328 0.458017 0.342552
11 0.983250 0.302190 0.439821 0.527750
12 0.348954 0.191272 0.402686 0.692401
13 0.623846 0.846459 0.376363 0.263270
14 0.786151 0.113710 0.585870 0.633940
15 0.394097 0.452079 0.303508 0.796536
16 0.256991 0.829933 0.386324 0.994061
17 0.163573 0.158677 0.156570 0.641327
18 0.255664 0.855975 0.217292 0.274872
19 0.938094 0.145893 0.353586 0.740469
输出df1
a b c d
0 0.175034 0.491546 0.214332 0.551543
1 0.360353 0.529040 0.949357 0.990295
2 0.475146 0.684860 0.252388 0.319754
3 0.296353 0.668867 0.806810 0.303479
4 0.125018 0.195096 0.470490 0.535244
5 0.279616 0.888702 0.925141 0.142643
6 0.713421 0.762474 0.332148 0.546850
7 0.908884 0.247675 0.008861 0.291648
8 0.002747 0.513682 0.609479 0.215230
9 0.789802 0.270530 0.390524 0.356701
出df2
a b c d
10 0.621201 0.502328 0.458017 0.342552
11 0.983250 0.302190 0.439821 0.527750
12 0.348954 0.191272 0.402686 0.692401
13 0.623846 0.846459 0.376363 0.263270
14 0.786151 0.113710 0.585870 0.633940
15 0.394097 0.452079 0.303508 0.796536
16 0.256991 0.829933 0.386324 0.994061
17 0.163573 0.158677 0.156570 0.641327
18 0.255664 0.855975 0.217292 0.274872
19 0.938094 0.145893 0.353586 0.740469
答案 1 :(得分:0)
您可以从集合A中随机采样66,933个值:
random.sample(SetB, len(setA))
答案 2 :(得分:0)
您可以在两个集合上使用熊猫插值,然后从两个集合中选择特定时间值(或频率)的值。如果您知道特定问题中的温度最有可能线性变化,则最好采用取平均值的方法。
a = [(time1, value1) , (time2, value2) , .....] # 375638 values
b = [(TIME1, VALUE1) , (TIME2, VALUE2) , .....] # 66933 values
da = pd.DataFrame( a , columns = ['times', 'values']).set_index('times')
db = pd.DataFrame( b , columns = ['TIMES', 'VALUES']).set_index('TIMES')
da2 = pd.Series(da['values'], index = da.index)
db2 = pd.Series(db['VALUES'], index = db.index)
interpol_a = da2.resample('S').interpolate(method='linear') # 'S' for interpolation in seconds
interpol_b = db2.resample('S').interpolate(method='linear')
interp_values_a = interpol_a.loc['index']
interp_values_b = interpol_b.loc['index'] # 'index' is the index at which you want the interpolated
# values which in your case will be the time instant or
# you can use a list of instants using proper indexing
# Feed same time instants to both interpol.loc[] methods to get temperature values at same instants for both datasets
答案 3 :(得分:0)
通过获取每秒或可能是线性插值的平均值,但是我不确定如何做到这一点
假设这些不是集合,而是按时间排序的列表,并且您不想要任何外部库(例如已经建议的Pandas),这是一个基本概念的快速代码:
current_time_bound = start_time
time_interval = 1
current_index = 0
counter = 0
sum = 0
while current_index < len(your_list):
if your_list[current_index].time < current_time_bound:
counter+=1
sum+=your_list[current_index].value
current_index+=1
else:
average = sum/counter #remember to check the counter because it might be 0
#and append the average to the new list
counter=0 #reset variables for the next group
sum=0
current_time_bound+=time_interval #move the time forward
当然,时间必须采用您使用的任何格式,并以该格式添加/比较。这只是一个普遍的想法。
这些事情可能可以以纯粹的功能方式完成(我记得在Scala中看到过类似的事情),但这可能会令人困惑。