pandas 从csv文件中扫描并找到数据库中的关键词,然后计算其他词的出现率

gkl3eglg  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(154)

我需要找到存在率/流行的话在csv文件中由逗号分隔,为旁边的某个关键字的话行。

import pandas as pd
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")

searchDB = pd.read_csv('')
searchDB = searchDB["AllKeywords"].str.split(', ')
searchDB = searchDB.explode()

df = pd.read_csv('') // keywords to look for

for i in range(len(df)):
    keywordToSearch = df.loc[i, "H"]
    res = es.search(index=searchDB["AllKeywords"], body={"from":0, "size":0, "query":{"match":{"sentence": df.loc[i, "H"]}}})

我在使用Elasticsearch的最后几行遇到错误。你能帮我吗?

Traceback (most recent call last):
  File "/Users//PycharmProjects/DataImp/venv/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3629, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 144, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index_class_helper.pxi", line 41, in pandas._libs.index.Int64Engine._check_type
KeyError: 'AllKeywords'
dldeef67

dldeef671#

您的错误似乎位于index=searchDB["AllKeywords"]
清除变量

import pandas as pd
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")

df = pd.read_csv('')
keywords = df["AllKeywords"].str.split(', ')
exploded_keywords = searchDB.explode()

for i in range(len(df)):
    keywordToSearch = df.loc[i, "H"]
    res = es.search(index=df["AllKeywords"], body={"from":0, "size":0, "query":{"match":{"sentence": keywordToSearch}}})

相关问题