Python 检查数据帧中是否有特定的字

Python 检查数据帧中是否有特定的字,python,pandas,Python,Pandas,我的任务是用python编写代码,检查数据帧中是否有单词。我使用了regex函数,但结果不正确,因为它只在列名中找到匹配项,而在行中找不到匹配项。如果有人能帮助我,我将不胜感激 import re df=pd.read_csv(r'C:\Users\Nenad\Desktop\Snowflake_databases\DRAGANA_DB\Dragana_db_information.csv') for i in df: x=re.match('TABLE.*', i)

我的任务是用python编写代码,检查数据帧中是否有单词。我使用了regex函数,但结果不正确,因为它只在列名中找到匹配项,而在行中找不到匹配项。如果有人能帮助我,我将不胜感激

import re
df=pd.read_csv(r'C:\Users\Nenad\Desktop\Snowflake_databases\DRAGANA_DB\Dragana_db_information.csv')

for i in df:
      x=re.match('TABLE.*', i)
      if x:
       print(i)
您可以使用df.loc:

df.loc[df.values=='YourWordHere']
或者,如果您有一个单词列表,并且希望创建一个新的数据框,其中只包含这些单词的行:

l = ['Words', 'to', 'search']
cat = pd.DataFrame()

for w in l:
    result = df.loc[df.values==w]
    cat = pd.concat([cat, result])

下面的代码将帮助您在要搜索的特定列中查找单词

import csv

csvfile = open("C:\Users\Nenad\Desktop\Snowflake_databases\DRAGANA_DB\Dragana_db_information.csv")
reader = csv.DictReader(csvfile)
for row in reader:
    if "Table" in row["<Column Name>"]:
        print row

请提供示例数据来重现您的问题,好吗?当您在df中为i循环
时,它会遍历每个列名。您需要循环遍历行,如
for i in range(len(df))…
,然后按
i
进行过滤。另外,尝试使用Pandas中的
str.extract()
对于df中的i:
将迭代数据帧的列索引,而不是数据本身。IIUC,您可以尝试
df.stack().str.contains('Table.*').unstack().any(axis=0)
非常感谢!非常感谢你!
import csv

csvfile = open("C:\Users\Nenad\Desktop\Snowflake_databases\DRAGANA_DB\Dragana_db_information.csv")
reader = csv.DictReader(csvfile)
for row in reader:
    if "Table" in str(row):
        print row