[英]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.