第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據(jù)子字符串列表過濾 Pandas 數(shù)據(jù)框

根據(jù)子字符串列表過濾 Pandas 數(shù)據(jù)框

忽然笑 2022-05-19 14:11:16
我有一個包含多個字符串列的 Pandas 數(shù)據(jù)框。我現(xiàn)在想根據(jù)允許的子字符串列表檢查某個列,然后得到一個帶有結(jié)果的新子集。substr = ['A', 'C', 'D']df = pd.read_excel('output.xlsx')df = df.dropna()# now filter all rows where the string in the 2nd column doesn't contain one of the substrings我發(fā)現(xiàn)的唯一方法是創(chuàng)建相應(yīng)列的列表,然后進行列表理解,但隨后我松開了其他列。我可以使用列表理解作為 eg 的一部分df.str.contains()嗎?year  type     value   price2000  ty-A     500     100002002  ty-Q     200     846002003  ty-R     500     560002003  ty-B     500     180002006  ty-C     500     125002012  ty-A     500     650002018  ty-F     500     860002019  ty-D     500     51900預(yù)期輸出:year  type     value   price2000  ty-A     500     100002006  ty-C     500     125002012  ty-A     500     650002019  ty-D     500     51900
查看完整描述

2 回答

?
犯罪嫌疑人X

TA貢獻2080條經(jīng)驗 獲得超4個贊

你可以使用pandas.Series.isin


>>> df.loc[df['type'].isin(substr)]

   year type  value  price

0  2000    A    500  10000

4  2006    C    500  12500

5  2012    A    500  65000

7  2019    D    500  51900


查看完整回答
反對 回復(fù) 2022-05-19
?
FFIVE

TA貢獻1797條經(jīng)驗 獲得超6個贊

你可以使用pandas.DataFrame.anypandas.DataFrame.all

如果你想要所有實例匹配的地方

df.loc[df['type'].apply(lambda x: all( word in x for word in substr)

或者如果你想要任何來自 substr

df.loc[df['type'].apply(lambda x: any( word in x for word in substr)

如果您打印或返回 df 過濾列表,則應(yīng)該這樣做。


查看完整回答
反對 回復(fù) 2022-05-19
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號