【问题标题】:search within a certain coumn in a pandas DataFrame for string and then print the whole row在 pandas DataFrame 的某一列中搜索字符串,然后打印整行
【发布时间】:2019-05-04 02:39:30
【问题描述】:

我导入了一个 txt 文件并将其转换为 pandas DataFrame。 (pd.DataFrame(pd.read_csv) 我想在一列中搜索某种模式,如下所示:t=0。例如“文本文本文本文本 t=10 文本文本”。等号后的数字会有所不同按行,我希望打印包含此模式的所有行。我通过应用正则表达式进行了尝试,但出现以下消息: TypeError:预期的字符串或类似字节的对象

你能帮帮我吗?

最好的问候

【问题讨论】:

标签: python regex pandas dataframe


【解决方案1】:

使用str模块中的contains函数:

mask = df['column_with_text'].str.contains('t=0', na =False,regex=True)

# keep columns matching what we want
df[mask]['column_with_text']

您可以从那里选择循环打印它们。

【讨论】:

  • 非常感谢。出于好奇,如果我想提取一个组,为什么我不能使用 extract 而不是 contains?当我尝试它时,我收到错误消息:extract() 得到了一个意外的关键字参数“na”。如何在处理 naN 值的同时提取组?
猜你喜欢
  • 1970-01-01
  • 2015-09-11
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 2020-10-13
  • 1970-01-01
相关资源
最近更新 更多