简体   繁体   English

如何使用通配符在 DataFrame 中搜索特定字符串

[英]How to search a DataFrame for a specific string using a wildcard

I have a DataFrame that has a column that I need to search using a wildcard.我有一个 DataFrame 有一个我需要使用通配符搜索的列。 I tried this:我试过这个:

df = pd.read_excel('CHQ REG.xlsx',index=False)
df.sort_values(['CheckNumber'], inplace=True)
df[df.CheckNumber.str.match('888')]
df

This returns everything in by df这将返回 df 中的所有内容

Here is my goal:这是我的目标:

CheckBranch  CheckNumber
  Lebanon      8880121

Sample:样本:

CheckBranch     CheckNumber
  Texas            4782436
  Georgia          8967462
  Lebanon          8880121
  China            8947512

Try:尝试:

res = df[df['CheckNumber'].astype('string').str.match('888')]
print(res)

Output Output

  CheckBranch  CheckNumber
2     Lebanon      8880121

As an alternative:作为备选:

res = df[df['CheckNumber'].astype('string').str.startswith('888')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM