[英]Pandas Groupby Faster Alternative?
My merged dataframe looks like:我合并的 dataframe 看起来像:
df =
Datetimes Symbol Open High Low Close Volume
0 2020-04-15 20MICRONS 26.60 31.40 25.60 27.85 75893
0 2020-04-16 20MICRONS 28.00 28.65 24.65 26.80 87254
0 2020-04-17 20MICRONS 28.80 29.00 26.80 28.75 81116
0 2020-04-15 33MICRONS 26.60 31.40 25.60 27.85 75893
0 2020-04-16 33MICRONS 28.00 28.65 24.65 26.80 87254
0 2020-04-17 33MICRONS 28.80 29.00 26.80 28.75 81116
I want to check the volume rise daily for every symbol.我想每天检查每个交易品种的成交量上升情况。
I came up with this:我想出了这个:
def checkvol(tf):
tf['vol'] = tf.Volume/tf.Volume.shift(1)
return tf
df = df.groupby('Symbol').apply(checkvol)
Is there any faster alternative to it - I also apply other functions to my df sliced by symbol.有没有更快的替代方案 - 我还将其他功能应用于按符号切片的 df 。
You can avoid df.apply
like this:您可以像这样避免
df.apply
:
In [158]: df['vol'] = df.Volume.div(df.groupby('Symbol')['Volume'].shift(1))
In [159]: df
Out[159]:
Datetimes Symbol Open High Low Close Volume vol
0 2020-04-15 20MICRONS 26.6 31.40 25.60 27.85 75893 NaN
0 2020-04-16 20MICRONS 28.0 28.65 24.65 26.80 87254 1.149698
0 2020-04-17 20MICRONS 28.8 29.00 26.80 28.75 81116 0.929654
0 2020-04-15 33MICRONS 26.6 31.40 25.60 27.85 75893 NaN
0 2020-04-16 33MICRONS 28.0 28.65 24.65 26.80 87254 1.149698
0 2020-04-17 33MICRONS 28.8 29.00 26.80 28.75 81116 0.929654
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.