我有一個資料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
...................
..................
如果第一個值(一個字串)col4在每個資料幀list(df1 ,df2,df3....df10)不與任何值相匹配col2 的DATA_SEL,我想洗掉df的list。我怎么可能這樣做?
另外,如果我想建立一個新的串列,list2其中第一個值(字串)col4在每個資料幀list(df1 ,df2,df3....df10)與任何值匹配col2 的DATA_SEL,那怎么辦?
uj5u.com熱心網友回復:
使用串列理解和過濾:
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()]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367480.html
