擅长:python、mysql、java
<p>您可以创建一个临时列,该列包含掩码列<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html" rel="nofollow noreferrer">split</a>、<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.explode.html" rel="nofollow noreferrer">explode</a>,并仅筛选与临时列的类匹配的行:</p>
<pre><code>(df
#create temporary column
.assign(mask_expand = lambda x: x['mask'].str.split('|'))
# 'explode' column into individual elements per row
.explode('mask_expand')
#keep only rows where the values in classes are in mask_expand(the temporary column)
.query('classes == mask_expand')
#get rid of the temporary column
.drop('mask_expand', axis = 1)
)
student classes money mask
0 6 psp -26.23 adj|psp
5 6 adj 9.55 adj|psp
6 6 psp -18.56 adj|psp
9 6 psp 27.41 adj|psp
10 89 alw -180.33 alw
11 89 alw -869.44 alw
15 89 alw 180.23 alw
17 105 tnt -24.01 tnt
20 105 tnt 60.81 tnt
</code></pre>