81

Looks ugly:

df_cut = df_new[
             (
             (df_new['l_ext']==31) |
             (df_new['l_ext']==22) |
             (df_new['l_ext']==30) |
             (df_new['l_ext']==25) |
             (df_new['l_ext']==64)
             )
            ]

Does not work:

df_cut = df_new[(df_new['l_ext'] in [31, 22, 30, 25, 64])]

Is there an elegant and working solution of the above "problem"?

4

2 回答 2

174

使用伊辛

df_new[df_new['l_ext'].isin([31, 22, 30, 25, 64])]
于 2013-08-15T10:05:29.127 回答
10

您可以使用pd.DataFrame.query

select_values = [31, 22, 30, 25, 64]
df_cut = df_new.query('l_ext in @select_values')

在后台,这使用了顶级pd.eval函数。

于 2018-10-08T16:33:27.620 回答