简体   繁体   中英

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. 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

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

  CheckBranch  CheckNumber
2     Lebanon      8880121

As an alternative:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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