如果元素存在于数据框的另一列上,则删除列表元素
deleting list element if the element is present on another column of a dataframe
我有一个数据框列表 list=[df1,df2,df3,df4,...df10]
数据框的构造如下:
>df1
col1 col2 col3 col4
Y 2 XX PP
我有另一个数据框DATA_SEL
这样
>DATA_SEL
col1 col2 col3 col4
A KK C D
A1 PP C D
...................
..................
如果 list
中每个数据帧的 col4
的第一个值(字符串)(df1 ,df2,df3....df10
)与 col2
中的任何值都不匹配 DATA_SEL
,我想从 list
中删除 df
。
我怎么可能那样做?
此外,如果我想构建一个新列表,list2
其中 col4
中每个数据帧的第一个值(字符串) list
(df1 ,df2,df3....df10
)与 col2
in DATA_SEL
中的任意值匹配,该怎么做?
使用带过滤的列表理解:
L = [df1,df2,df3,df4,...df10]
#tested first value of col4
out = [x for x in L if DATA_SEL['col2'].eq(x.at[x.index[0], 'col4']).any()]
#if first row has index == 0
out = [x for x in L if DATA_SEL['col2'].eq(x.at[0, 'col4']).any()]
#tested any value of col4
out = [x for x in L if DATA_SEL['col2'].isin(x['col4']).any()]
我有一个数据框列表 list=[df1,df2,df3,df4,...df10]
数据框的构造如下:
>df1
col1 col2 col3 col4
Y 2 XX PP
我有另一个数据框DATA_SEL
这样
>DATA_SEL
col1 col2 col3 col4
A KK C D
A1 PP C D
...................
..................
如果 list
中每个数据帧的 col4
的第一个值(字符串)(df1 ,df2,df3....df10
)与 col2
中的任何值都不匹配 DATA_SEL
,我想从 list
中删除 df
。
我怎么可能那样做?
此外,如果我想构建一个新列表,list2
其中 col4
中每个数据帧的第一个值(字符串) list
(df1 ,df2,df3....df10
)与 col2
in DATA_SEL
中的任意值匹配,该怎么做?
使用带过滤的列表理解:
L = [df1,df2,df3,df4,...df10]
#tested first value of col4
out = [x for x in L if DATA_SEL['col2'].eq(x.at[x.index[0], 'col4']).any()]
#if first row has index == 0
out = [x for x in L if DATA_SEL['col2'].eq(x.at[0, 'col4']).any()]
#tested any value of col4
out = [x for x in L if DATA_SEL['col2'].isin(x['col4']).any()]