我将一个数据帧读入字典。之前,我过滤掉了所有.isin(['I1','I2','I3','I4'])
的行。
df = {}
df['cgo_eafapo_t'] = pd.read_sql_table('cgo_eafapo_t', engine, schema_z2,
columns = cols)
df['cgo_eafapo_t'] = df['cgo_eafapo_t'][df['cgo_eafapo_t']['art_kennz'].isin(['I1','I2','I3','I4'])]
然而,现在我想过滤掉所有不在(['I1','I2','I3','I4'])
中的行。我该怎么做呢?我知道我们可以使用~
,但我不确定确切的位置。我试过了,但它抛出了一个类型错误:
df['cgo_eafapo_t'] = ~df['cgo_eafapo_t'][df['cgo_eafapo_t']['art_kennz'].isin(['I1','I2','I3','I4'])]
ufunc 'invert' not supported for the input types, and the inputs
还尝试将符号放在art_kennz之前,但也抛出了错误
df['cgo_eafapo_t'] = df['cgo_eafapo_t'][df['cgo_eafapo_t'](~['art_kennz'].isin(['I1','I2','I3','I4']))]
attribute error list object has no attribute isin
这个也不工作:
df['cgo_eafapo_t'] = df['cgo_eafapo_t']~([df['cgo_eafapo_t']['art_kennz'].isin(['I1','I2','I3','I4'])])
使用~
前条件:
df['cgo_eafapo_t'] = df['cgo_eafapo_t'][~df['cgo_eafapo_t']['art_kennz'].isin(['I1','I2','I3','I4'])]
Better是由DataFrame.loc
:
df['cgo_eafapo_t'] = df.loc[~df['cgo_eafapo_t']['art_kennz'].isin(['I1','I2','I3','I4']), 'cgo_eafapo_t']