[英]subset df using conditionals - pandas
I'm aiming to subset a df using two conditions.我的目标是使用两个条件对 df 进行子集化。 Those being, return rows only when string values in L2
are after by a value in L1
and are followed by a value in L1
.这些之中,返回行仅在字符串值L2
是通过在值之后L1
,并且随后在值L1
。
df = pd.DataFrame({
'col_1' : ['a','m','x','b','n','c','c','o','y','a','m','c'],
})
L1 = ['a','b','c']
L2 = ['m','n','o']
L3 = ['x','y','z']
m1 = df['col_1'].isin(L1) & df['col_1'].shift(-1).isin(L2)
m2 = df['col_1'].isin(L2) & df['col_1'].shift().isin(L1)
df = df[m1 | m2 ].reset_index(drop = True)
intended output:预期输出:
col_1
4 n
10 m
you can try:你可以试试:
df[(df['col_1'].isin(L1).shift() & df['col_1'].isin(L2)) & \
(df['col_1'].isin(L1).shift(-1) & df['col_1'].isin(L2))]
Output:输出:
col_1
4 n
10 m
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.