我將資料幀讀入字典。早些時候,我過濾掉了所有.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'])]
但是,現在我想過濾掉所有 NOT IN 的行(['I1','I2','I3','I4'])。我怎么能這樣做?我知道我們可以使用 a~但我不確定具體在哪里。我試過這個,但它拋出了一個型別錯誤:
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'])])
uj5u.com熱心網友回復:
使用~前條件:
df['cgo_eafapo_t'] = df['cgo_eafapo_t'][~df['cgo_eafapo_t']['art_kennz'].isin(['I1','I2','I3','I4'])]
更好的是選擇DataFrame.loc:
df['cgo_eafapo_t'] = df.loc[~df['cgo_eafapo_t']['art_kennz'].isin(['I1','I2','I3','I4']), 'cgo_eafapo_t']
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338376.html
上一篇:為什么這段代碼輸出錯誤的答案?
