[英]Remove consecutive positive/negative numbers from a dataframe
(EDITED) This is my current DataFrame: (已编辑)这是我当前的 DataFrame:
aapl tigr srpt
4 58.254690 2475.247525 131.665569
5 56.869882 2386.634845 140.016802
6 -58.709564 -2597.402597 NaN
7 NaN 2314.814815 145.539223
8 -60.786578 NaN -154.822728
9 -57.780089 -2283.105023 -140.646976
10 57.116747 2192.982456 130.992926
11 58.139535 2304.147465 115.074799
12 -54.942036 -2074.688797 -110.595001
I was wondering if there is any way possible to remove all but the 1st consecutive positive/negative number, such that the result is the DataFrame below:我想知道是否有任何方法可以删除除第一个连续正数/负数以外的所有数,这样结果就是下面的 DataFrame:
aapl tigr srpt
4 58.254690 2475.247525 131.665569
6 -58.709564 -2597.402597 NaN
7 NaN 2314.814815 145.539223
8 -60.786578 NaN -154.822728
10 57.116747 2192.982456 130.992926
12 -54.942036 -2074.688797 -110.595001
Use:利用:
In [1481]: x = df.fillna(1)['aapl'].gt(0)
In [1487]: ix = x[~x.eq(x.shift(1))].index
In [1488]: df.loc[ix]
Out[1488]:
aapl tigr srpt
4 58.254690 2475.247525 131.665569
6 -58.709564 -2597.402597 -143.492610
7 NaN 2314.814815 145.539223
8 -60.786578 -2032.520325 -154.822728
10 57.116747 2192.982456 130.992926
12 -54.942036 -2074.688797 -110.595001
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.