简体   繁体   English

从 dataframe 中删除连续的正数/负数

[英]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.

相关问题 如何从 dataframe 的列表中同时删除正数和负数? - How to remove both positive and negative numbers from a list in dataframe? 如何使只有连续的负数从混合正数的列表中相互添加 - How to make only consecutive negative numbers add each other from a list mixed with positive numbers 用正数创建数据框行,用负数创建其他行 - Create dataframe row with positive numbers and other with negative 如何找到列表中连续正数和负数的平均长度? - How to find the average length of consecutive positive and negative numbers in a list? 在有正数和负数的 dataframe 中,如何将正数向上舍入和将负数向下舍入(均为 5 的倍数)? - In a dataframe with both positive & negative numbers, how to round positive numbers up and round negative numbers down (both to multiples of 5)? 如何从 Pandas Dataframe 中删除连续的相反数字对? - How to remove consecutive pairs of opposite numbers from Pandas Dataframe? range() 函数中的负数 - 如何从正数打印到负数? - Negative numbers in range() function - how to print from positive to negative numbers? Python切片包含从负数到正数的包装 - Python slicing with wrapping from negative to positive numbers plot 从 pandas dataframe 具有负值和正值 - plot from pandas dataframe with negative and positive values 从矩阵中删除负数 - Remove negative numbers from a matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM