【问题标题】:Creating a list with of documents that contain certain words in Python [duplicate]在Python中创建包含某些单词的文档列表[重复]
【发布时间】:2021-07-31 16:27:01
【问题描述】:

我有一个 CSV 文件,其中包含以下列:

df =
title body
A     yellow, gree
B     blue, red
C     red, green
D     grey, blue

我想创建一个包含红色单词的行的新数据框:

df_New =
title body
B     blue, red
C     red, green

到目前为止,我的代码如下所示:

import pandas

Documents = pandas.read_csv(df.csv)

words = ['red', 'Red']

Df_New = Documents.str.match('(words)')

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以使用.str.contains()

    df_ = df[df['body'].str.contains('red', case=False)]
    
    print(df_)
    
      title        body
    1     B   blue, red
    2     C  red, green
    

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多