简体   繁体   English

将计数列添加到数据框,该数据框计算另一行何时更改

[英]Add count column to dataframe that counts when when another row changes

I have a dataframe that has a column like this: 我有一个数据框,其中包含如下列:

      x
0     1
1     1
2     0
3     1
4     0
5     0
6     0
7     1
8     1
9     1

I'd like to add a column that counts up every time x changes so that my final result looks like this: 我想添加一个每次x更改时都会计数的列,以便我的最终结果如下所示:

      x     y
0     1     0
1     1     0
2     0     1
3     1     2
4     0     3
5     0     3
6     0     3
7     1     4
8     1     4
9     1     4

I can't figure out the fastest way to do this without looping. 如果没有循环,我无法找到最快的方法。 I also don't care if y starts at 0 or 1. I'm sure there's something innate to pandas I can use. 如果y从0或1开始,我也不在乎。我确信我可以使用的是大熊猫的天赋。 Can you help? 你能帮我吗?

PS. PS。 the reason I need to make this y column is do be able to group the rows by each number, if there's a way to essentially accomplish the same thing without creating it, that would work too. 我需要制作这个y列的原因是能够按每个数字对行进行分组,如果有一种方法可以基本上完​​成同样的事情而不创建它,那也可以。

After diff you can apply cumsum diff你可以应用cumsum

df.x.diff().ne(0).cumsum()-1
Out[132]: 
0    0
1    0
2    1
3    2
4    3
5    3
6    3
7    4
8    4
9    4
Name: x, dtype: int32

With Numpy arrays 使用Numpy阵列

Note : This generalizes to object dtype as well since we are evaluating equality. 注意 :这也是object dtype的推广,因为我们正在评估相等性。

df.assign(y=np.append(False, df.x.values[1:] != df.x.values[:-1]).cumsum())

   x  y
0  1  0
1  1  0
2  0  1
3  1  2
4  0  3
5  0  3
6  0  3
7  1  4
8  1  4
9  1  4

暂无
暂无

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

相关问题 熊猫中是否有一种方法可以在一个数据帧中计数(Excel中的Countifs)并在另一个长度不同的数据帧中将计数添加为新列? - Is there a way in Pandas to count (Countifs in excel) in one dataframe and add counts as new column in another dataframe of different length? 与另一个 dataframe 相比,有没有办法找到行中的单元格何时发生变化? - Is there a way to find when a cell in a row changes compared to another dataframe? 是否可以在 dataframe 中添加一个计数器,该计数器每行计数并在两列之一中的值发生更改时重置为 1? - Is it possible to add a counter in a dataframe which counts per row and resets to 1 when a value in one of two columns is changed? Python - Pandas - Dataframe 如何在使用.count时将notnull添加到列 - Python - Pandas - Dataframe How to add notnull to a column when using .count Python - Pandas - Dataframe 如何在使用.count时将变量添加到列 - Python - Pandas - Dataframe How to add variables to a column when using .count 数据框如何在给定列中找到字符串时添加特定行 - dataframe how to add a specific row when a string is found in a given column 当列的条目重复时,在 dataframe 中添加一个空行 - Add an empty row in a dataframe when the entries of a column repeats 创建 Pandas DataFrame 时向行和列名称添加“名称”属性 - Add 'name' properties to row and column names when creating a pandas DataFrame Python dataframe 计算一行中的值并将状态添加到另一行 - Python dataframe count the values in a row and add status to the another row 数据框添加按列条件计数的列 - Dataframe add column that counts by column condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM