如何通过比较 Panda 数据框中的两个嵌套连续行来创建新列?

How to create a new column as a result of comparing two nested consecutive rows in the Panda dataframe?

我需要在 Panda Dataframe 中编写代码。所以:会依次检查ID列中的值是否相同。这里出现三种情况。情况一:如果ID与下一行不相同,则在Comment栏中写为“unique”。情况2:如果ID与下一栏相同,与下一栏不同,则在Comment栏中写成“ring”。情况三:如果ID与后面的多个列相同,则在Comment列中写成“multi”。情况 4:这样做直到 ID 列中的行完成。

import pandas as pd

df = pd.read_csv('History-s.csv')

a = len(df['ID'])
c = 0
while a != 0:
 c += 1
    while df['ID'][i] == df['ID'][i + 1]:
        if c == 2:
           if df['Nod 1'][i] == df['Nod 2'][i + 1]:
               df['Comment'][i] = "Ring"
               df['Comment'][i + 1] = "Ring"
          else:
               df['Comment'][i] = "Multi"
               df['Comment'][i + 1] = "Multi"
         elif c > 2:
             df['Comment'][i] = "Multi"
             df['Comment'][i + 1] = "Multi"
        i += 1
   else:
        df['Comment'][i] = "Unique"

一=一-1 打印(df,'\n')

Data is like this: Data After coding data frame should be like this: Result

根据您提供的输入数据框,我的第一印象是当您在 while 循环中检查 next 行时,您严格考虑的只是下一个 comin 行,例如

ID value comment
1 2 MULTI
1 3 RING
3 4 UNIQUE

但如果不是这种情况,您可以简单地使用 pandas groupby 函数。

def func(df):
    if len(df)>2:
        df['comment'] = 'MULTI'
    elif len(df)==2:
        df['comment'] = 'RING'
    else:
        df['comment'] = 'UNIQUE'
    return df

df = df.groupby(['ID']).apply(func)

输出:

   ID   value   comment
0   1   2       RING
1   1   3       RING
2   3   4       UNIQUE