简体   繁体   English

如果两列在 pandas Dataframe 中具有相似的值,如何写“I”?

[英]How to write 'I' if two columns are having similar values in pandas Dataframe?

Dataframe is like this: Dataframe是这样的:

            RS                 AS                    IS
F1  [F1, F2, F3, F4, F5]      [F1]                  [F1]
F2  [F2, F3, F5]          [F1, F2, F3, F5]      [F5, F3, F2]
F3  [F2, F3, F4, F5]      [F1, F2, F3, F5]      [F5, F3, F2]
F4  [F4]                  [F1, F3, F4, F5]          [F4]
F5  [F2, F3, F4, F5]      [F1, F2, F3, F5]      [F5, F3, F2]

Output I need: Output 我需要:

            RS                 AS                    IS          Level
F1  [F1, F2, F3, F4, F5]      [F1]                  [F1]     
F2  [F2, F3, F5]          [F1, F2, F3, F5]      [F5, F3, F2]       I
F3  [F2, F3, F4, F5]      [F1, F2, F3, F5]      [F5, F3, F2]       
F4  [F4]                  [F1, F3, F4, F5]          [F4]           I
F5  [F2, F3, F4, F5]      [F1, F2, F3, F5]      [F5, F3, F2]       

The logic is very simple.逻辑很简单。 If RS and IS is having similar values then write I in Level column.如果 RS 和 IS 具有相似的值,则在 Level 列中写入I I am using the following code but looks like it doesn't work.我正在使用以下代码,但看起来它不起作用。

if df['RS'].any() == df['IS'].any():
df['Level'] = 'I'

Also need to drop the entire row having level 'I' from original Dataframe after above method is implemented.实施上述方法后,还需要从原始 Dataframe 中删除级别为“I”的整行。 like this像这样

            RS                 AS                    IS
F1  [F1, F2, F3, F4, F5]      [F1]                  [F1]
F3  [F2, F3, F4, F5]      [F1, F2, F3, F5]      [F5, F3, F2]
F5  [F2, F3, F4, F5]      [F1, F2, F3, F5]      [F5, F3, F2]

Convert your lists to set and then comparing for equality to get which rows have the same elements, then assign the value.将您的列表转换为set ,然后比较是否相等以获取哪些行具有相同的元素,然后分配值。 The example below ignores your middle column.下面的示例忽略了您的中间列。

import pandas as pd

df = pd.DataFrame({'RS':
    [[1,2,3,4,5],
     [2,3,5],
     [2,3,4,5],
     [4],
     [2,3,4,5]],
    'IS':
    [[1],
     [5,3,2],
     [5,3,2],
     [4],
     [5,3,2]]})

ix = df.RS.apply(set) == df.IS.apply(set)
df['Level'] = ''
df.loc[ix, 'Level'] = 'I'

df:
# returns:
             RS         IS Level
[1, 2, 3, 4, 5]        [1]
      [2, 3, 5]  [5, 3, 2]     I
   [2, 3, 4, 5]  [5, 3, 2]
            [4]        [4]     I
   [2, 3, 4, 5]  [5, 3, 2]

If you need to drop the rows where I would be assigned;如果您需要删除分配给I的行; you don't actually need to assign I , just use:您实际上不需要分配I ,只需使用:

ix = df.RS.apply(set) == df.IS.apply(set)
df.loc[~ix]

暂无
暂无

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

相关问题 如何在某些列上获取具有相似值的数据帧子集? - how to get dataframe subsets having similar values on some columns? 如何显示 pandas dataframe 中两列中的相似词 - How to show similar word from two columns in pandas dataframe Jupyter notebooks / Pandas dataframe 如何处理具有相似值的多个列 - Jupyter notebooks / Pandas dataframe how to deal with multiple columns with similar values 用相似的列乘以两个熊猫数据框 - Multiply two pandas dataframe with similar columns 如何将具有相似名称的列的 pandas dataframe 转换为行? - How can I turn a pandas dataframe with columns with similar names into rows? 如何合并 pandas dataframe 中具有相似名称的列? - How do I merge columns that have similar names in a pandas dataframe? 使用熊猫,如何在两列中过滤具有相似值的行 - Using pandas, how to filter rows with similar values in two columns 如何根据 pandas dataframe 中多个其他列的值添加两个新列? - How do I add two new columns on the basis of the values of multiple other columns in a pandas dataframe? Pandas Dataframe:如何比较一行的两列中的值是否等于后续行的同一列中的值? - Pandas Dataframe: how can i compare values in two columns of a row are equal to the ones in the same columns of a subsequent row? 如何在pandas数据帧的连续相似值上使用groupby? - How do I use groupby on continuous similar values for a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM