我在下面的python中有熊猫数据框。
full_name serial Date_YMD prc1 prc2 volume
bottle_a AX80 20200922 12874.50 12927.75 61023.0
bottle_a AX80 20200923 12878.50 12926.75 61023.0
bottle_a AX80 20200924 12872.50 12928.75 61023.0
bottle_a AX80 20200925 12885.50 12984.25 62295.0
bottle_a AX80 20200926 12880.00 13000.00 14224.0
首先,我想根据column来检测哪一行落在星期六Date_YMD
。然后,在周六找出音量值,volume
如果上一行的值大于当前行的值,则将该值分配给上一行。
上表的最终熊猫框架看起来像这样;
full_name serial Date_YMD prc1 prc2 volume
bottle_a AX80 20200922 12874.50 12927.75 61023.0
bottle_a AX80 20200923 12878.50 12926.75 61023.0
bottle_a AX80 20200924 12872.50 12928.75 61023.0
bottle_a AX80 20200925 12885.50 12984.25 14224.0
bottle_a AX80 20200926 12880.00 13000.00 14224.0
星期六的交易量可以通过下面的代码获得;
df['Date_YMD'] = pd.to_datetime(df['Date_YMD'], format='%Y%m%d')
sat_volume = df.loc[df['Date_YMD'].dt.dayofweek.eq(5), 'volume']
问题是,在比较值之后如何将其分配sat_volume
给上一行volume
?
我正在使用python 3.8
让我们创建布尔掩码m1
,m2
然后对这些掩码使用布尔索引,以填充满足和volume
指定的条件的列:m1
m2
m1 = df['volume'] > df['volume'].shift(-1)
m2 = df['Date_YMD'].shift(-1).dt.weekday.eq(5)
df.loc[m1 & m2, 'volume'] = df['volume'].shift(-1)
full_name serial Date_YMD prc1 prc2 volume
0 bottle_a AX80 2020-09-22 12874.5 12927.75 61023.0
1 bottle_a AX80 2020-09-23 12878.5 12926.75 61023.0
2 bottle_a AX80 2020-09-24 12872.5 12928.75 61023.0
3 bottle_a AX80 2020-09-25 12885.5 12984.25 14224.0
4 bottle_a AX80 2020-09-26 12880.0 13000.00 14224.0
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句