[英]Difference of 2 columns in pandas dataframe with some given conditions
I have a sheet like this.我有一张这样的床单。 I need to calculate absolute of "CURRENT HIGH" - "PREVIOUS DAY CLOSE PRICE" of particular "INSTRUMENT" and "SYMBOL".
我需要计算特定“仪器”和“符号”的“当前最高价”-“前一天收盘价”的绝对值。
So I used.shift(1) function of pandas dataframe to create a lagged close column and then I am subtracting current HIGH and lagged close column but that also subtracts between 2 different "INSTRUMENTS" and "SYMBOL". So I used.shift(1) function of pandas dataframe to create a lagged close column and then I am subtracting current HIGH and lagged close column but that also subtracts between 2 different "INSTRUMENTS" and "SYMBOL". But if a new SYMBOL or INSTRUMENTS appears I want First row to be NULL instead of subtracting current HIGH and lagged close column.
但是,如果出现新的 SYMBOL 或 INSTRUMENTS 我希望第一行是 NULL 而不是减去当前的 HIGH 和滞后的关闭列。
What should I do?我应该怎么办?
I believe you need if all days are consecutive per groups:我相信如果每组所有的日子都是连续的,你需要:
df['new'] = df['HIGH'].sub(df.groupby(['INSTRUMENT','SYMBOL'])['CLOSE'].shift())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.