简体   繁体   English

有没有办法根据现有列的当前和前一行值在每行的新列中分配增量值的数量?

[英]Is there a way to assign number of incremental value in a new column of each rows based current and previous row values of existing columns?

I have a dataframe, df and I want to add incremental ID number in a new column based on current and previous row of another column.我有一个数据框 df,我想根据另一列的当前行和前一行在新列中添加增量 ID 号。

data = [{'X': 6, 'Y': 1}, {'X': 6, 'Y': 1}, {'X': 7, 'Y': 0}, {'X': 7, 'Y': 0},
        {'X': 6, 'Y': 1}, {'X': 7, 'Y': 1}, {'X': 7, 'Y': 0}, {'X': 7, 'Y': 1} ] 

df = pd.DataFrame(data)

Now this is the condition,现在条件是这样

if Current X = Previous X & Current Y <> Previous Y,如果当前 X = 前一个 X & 当前 Y <> 前一个 Y,

Then ID_Number = New Incremented Number然后 ID_Number = 新增加的数字

OR或者

if Current X <> Previous X & Current Y = Previous Y,如果当前 X <> 前一个 X & 当前 Y = 前一个 Y,

Then ID_Number = New Incremented Number然后 ID_Number = 新增加的数字

OR或者

if Current X = Previous X & Current Y = Previous Y,如果当前 X = 前一个 X & 当前 Y = 前一个 Y,

Then ID_Number = Same as Previous ID_Number然后 ID_Number = 与之前的 ID_Number 相同

OR或者

if Current X <> Previous X & Current Y <> Previous Y,如果当前 X <> 前一个 X & 当前 Y <> 前一个 Y,

Then ID_Number = New Incremented Number然后 ID_Number = 新增加的数字

Input Dataframe will be look like this输入数据框将如下所示

enter image description here在此处输入图片说明

My expectation Output is this我的期望输出是这个

enter image description here在此处输入图片说明

Look a this snippet, don't forget to handle the first item.看看这个片段,不要忘记处理第一项。 copy paste from ipython.从 ipython 复制粘贴。

In [10]: id_ =1  
In [11]: for first, second in zip(data, data[1:]): 
    ...:     if first != second: 
    ...:         id_ += 1 
    ...:     print(f'{id_} {second}') 
    ...:      
    ...:                              

see itertools module for more.更多信息请参见 itertools 模块。

暂无
暂无

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

相关问题 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 根据多个条件将现有列的值分配给 Pandas 中的新列 - Assign value of existing column to new columns in pandas based on multiple conditions 有没有办法将列转置为行,每行删除前一行的第一个值并插入一个新的尾值? - Is there a way to transpose a column into rows with each row having the first value from the previous row deleted and a new tail value inserted? 根据当前行和上一行的数据为数据帧分配新列 - Assign a new column to dataframe based on data from current row and previous row 如何根据当前行值和上一行值的两个条件分配列值? - How to assign values of a column based on two conditions for current and previous row values? 根据其他列创建新列,并在创建新列时根据上一行更新新列的行的值 - Create new columns based on other columns and update the value of row of new column based on the previous row while creating the new column 基于2个分类列pandas数据框创建新的增量列 - create new column of incremental number based on 2 categorical columns pandas dataframe 如何查找两列中值的范围与当前行中的范围相交的前几行 - How to find the number of previous rows in which the range of values in two columns intersects with the range in the current row 将当前行和上一行的差异附加到新列,用于多列 - Appending to a new column the differences of current row and previous row, for multiple columns 具有增量编号的新列基于不同的列值(熊猫)进行初始化 - New columns with incremental numbers that initial based on a diffrent column value (pandas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM