Python 两个数据帧按天分组而不是按小时随机采样
我有两个数据帧,一个是价格,另一个是体积。它们都是每小时一次的,并且适用于相同的时间范围(一年) 每一天都是一个集合,在这个意义上,价值必须保持在一起。生成样本时,需要一整天。因此,样本将在该数据集中(例如2008年2月2日的24小时)。我想为dfP生成一个185天(50%)的样本集,并拥有同一天的数量,以便生成一个求和产品Python 两个数据帧按天分组而不是按小时随机采样,python,pandas,dataframe,random-sample,Python,Pandas,Dataframe,Random Sample,我有两个数据帧,一个是价格,另一个是体积。它们都是每小时一次的,并且适用于相同的时间范围(一年) 每一天都是一个集合,在这个意义上,价值必须保持在一起。生成样本时,需要一整天。因此,样本将在该数据集中(例如2008年2月2日的24小时)。我想为dfP生成一个185天(50%)的样本集,并拥有同一天的数量,以便生成一个求和产品 dfProduct = dfP_Sample * dfV_Sample 我不知道如何实现这一点。感谢您的帮助。听起来您希望得到每天的销售量和价格之和,然后将它们相乘 如果
dfProduct = dfP_Sample * dfV_Sample
我不知道如何实现这一点。感谢您的帮助。听起来您希望得到每天的销售量和价格之和,然后将它们相乘 如果是这种情况,请尝试以下方法。如果没有,请澄清你的问题
priceGroup = dfP.groupby(by=dfP.index.date).sum()
volumeGroup = dfV.grouby(by=dfV.index.date).sum()
dfProduct = priceGroup*volumeGroup
如果您只想查看特定的日期范围,请尝试
import datetime as datetime
dfProduct[np.logical_and(dfProduct.index > datetime.date(2006,08,09),dfProduct.index < datetime.date(2007,01,02))]
将datetime导入为datetime
dfProduct[np.logical_和(dfProduct.index>datetime.date(2006,08,09),dfProduct.index
听起来你希望得到每天的销售量和价格之和,然后将它们相乘
如果是这种情况,请尝试以下方法。如果没有,请澄清你的问题
priceGroup = dfP.groupby(by=dfP.index.date).sum()
volumeGroup = dfV.grouby(by=dfV.index.date).sum()
dfProduct = priceGroup*volumeGroup
如果您只想查看特定的日期范围,请尝试
import datetime as datetime
dfProduct[np.logical_and(dfProduct.index > datetime.date(2006,08,09),dfProduct.index < datetime.date(2007,01,02))]
将datetime导入为datetime
dfProduct[np.logical_和(dfProduct.index>datetime.date(2006,08,09),dfProduct.index
首先,我们将生成一列,该列引用一年中的日期索引,例如,2008-01-01
将被指定为1,因为它表示一年中的第一天,依此类推
day_order = [date.timetuple().tm_yday for date in dfP.index]
dfP['day_order'] = day_order
然后生成从1到365的随机日,这将表示一年中的日顺序,例如,如果您得到随机数1
,这表示2008-01-01
random_days = np.random.choice(np.arange(1 , 366) , size = 185 , replace=False)
然后根据我们之前创建的日顺序列,对原始数据帧进行切片,以仅从随机样本中获取值
dfP_sample = dfP[dfP.day_order.isin(random_days)]
然后你们可以合并索引上的两个帧,你们可以做任何你们想做的事情
final = pd.merge(dfP_sample , dfV , left_index=True , right_index=True)
final.head()
Out[47]:
Col1_x Col2_x Col3_x Col4_x day_order Col1_y Col2_y Col3_y Col4_y
2008-01-03 00:00:00 9 6 9 9 3 66 85 62 82
2008-01-03 01:00:00 5 8 9 8 3 54 89 65 98
2008-01-03 02:00:00 7 5 5 9 3 83 58 60 96
2008-01-03 03:00:00 9 5 7 6 3 59 54 67 78
2008-01-03 04:00:00 9 5 8 9 3 92 66 66 55
如果不想合并两个帧,可以在dfV
然后,您将在同一天从两个数据帧中获取样本。首先,我们将生成一列,该列引用一年中的日期索引,例如,
2008-01-01
将被指定为1,因为它表示一年中的第一天,依此类推
day_order = [date.timetuple().tm_yday for date in dfP.index]
dfP['day_order'] = day_order
然后生成从1到365的随机日,这将表示一年中的日顺序,例如,如果您得到随机数1
,这表示2008-01-01
random_days = np.random.choice(np.arange(1 , 366) , size = 185 , replace=False)
然后根据我们之前创建的日顺序列,对原始数据帧进行切片,以仅从随机样本中获取值
dfP_sample = dfP[dfP.day_order.isin(random_days)]
然后你们可以合并索引上的两个帧,你们可以做任何你们想做的事情
final = pd.merge(dfP_sample , dfV , left_index=True , right_index=True)
final.head()
Out[47]:
Col1_x Col2_x Col3_x Col4_x day_order Col1_y Col2_y Col3_y Col4_y
2008-01-03 00:00:00 9 6 9 9 3 66 85 62 82
2008-01-03 01:00:00 5 8 9 8 3 54 89 65 98
2008-01-03 02:00:00 7 5 5 9 3 83 58 60 96
2008-01-03 03:00:00 9 5 7 6 3 59 54 67 78
2008-01-03 04:00:00 9 5 8 9 3 92 66 66 55
如果不想合并两个帧,可以在dfV
然后,您将在同一天从两个数据帧中获取样本OP想要获取年日样本,这不是正确答案,请再次阅读问题OP想要获取年日样本,这不是正确答案,请再次阅读问题