繁体   English   中英

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

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

我有一个 DataFrame 有一个我需要使用通配符搜索的列。 我试过这个:

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

这将返回 df 中的所有内容

这是我的目标:

CheckBranch  CheckNumber
  Lebanon      8880121

样本:

CheckBranch     CheckNumber
  Texas            4782436
  Georgia          8967462
  Lebanon          8880121
  China            8947512

尝试:

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

Output

  CheckBranch  CheckNumber
2     Lebanon      8880121

作为备选:

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

暂无
暂无

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

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