简体   繁体   English

pandas - 查找一行中相等值的批次之间的索引距离

[英]pandas - find index distance between batches of equal values in a row

I would like to find the "distance" between the starting points of two batches of 1 's in a row or in other words the length of batches of " 1 's followed by 0 's" (indicated with spaces below).我想找到连续两批1的起点之间的“距离”,或者换句话说,“ 1后跟0 ”的批次长度(用下面的空格表示)。

So I start with the following series:所以我从以下系列开始:

df = pd.Series([0,0, 1,1,1,0,0,  1,1,0,  1,1,1,0,0,0,0,  1,1,1,0,0,0,  1,1,0,0])

and would like to get the following output:并希望获得以下输出:

0    NaN
1    5.0
2    3.0
3    7.0
4    6.0
5    NaN

I know how to get either the counts of the number of 1 's in a row or the counts of the number of 0 's in a row but I don't know how to deal with the case of this pattern of 1 's followed by 0 's as a pattern for its own...我知道如何获得连续1的数量或连续0的数量,但我不知道如何处理这种1模式的情况后跟0作为它自己的模式......

Having NaN's at the beginning and end would be the ideal case but is not necessary.在开头和结尾使用 NaN 是理想的情况,但不是必需的。

Use diff() to find the difference, 1 indicates starting of a new batch.使用diff()查找差异, 1表示开始新批次。 Then you can use np.diff on the index:然后你可以在索引上使用np.diff

s = df.diff().eq(1)
np.diff(s.index[s])

# or a one-liner
# np.diff(np.where(df.diff().eq(1))[0])

Output:输出:

array([5, 3, 7, 6])

Note There is an edge case where the series starts with a 1 .注意有一个边缘情况,其中系列以1开头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Pandas Dataframe 中找到 5 个连续的相等的行值 - Find 5 consecutive row values in Pandas Dataframe that are equal 如何基于另一列的值(其中两个不同的列中的值在pandas中相等)找到一列的行索引? - How to find row index of one column based on values of a different column where the values in the two distinct columns are equal in pandas? 熊猫:最后一个非相等行的索引 - Pandas: Index of last non equal row 用“真”找到最近的索引并计算距离(熊猫) - Find the closest index with "True" and calculating the distance (Pandas) 查找 pandas 中 2 个坐标之间的距离,并带有半正弦误差 - Find the distance between 2 coords in pandas with haversine error 计算熊猫数据框中列值之间的距离 - Calculating distance between column values in pandas dataframe 用行值替换Pandas索引 - Replacing Pandas index with row values Pandas DataFrame:查找两列相等/相同的特定长度序列的索引值 - Pandas DataFrame: Find index values for sequences of a certain length where two columns are equal/identical 仅保留列值,如果它们等于某个值或它们在此值之间的一行中(熊猫) - Only keep column values if they equal a certain value or if they are in a row between this value (pandas) 在 pandas 中:在两行之间插值,使得插值之和等于第二行 - In pandas: Interpolate between two rows such that the sum of interpolated values is equal to the second row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM