Получите данные от пользователя и напечатайте частоту этого термина из цикла данных, используя цикл for в python - PullRequest
0 голосов
/ 03 мая 2018

У меня есть названия авторских публикаций в CSV-файле. Я читаю это в Python, используя следующий код -

In[1]:

import pandas as pd
import numpy as np
data = pd.read_csv('C:\\Users\\orevo\\Desktop\\Python\\Publication Data\\author.csv')
df = pd.DataFrame(data)
df1 = df.iloc[:,3]
df1

, который дает мне этот вывод -

Out[1]:

0     The adhesins of non-typeable Haemophilus influ...
1     Head and Neck Squamous Cell Carcinomas Are Cha...
2     Multitissue Transcriptomics Delineates the Div...
3     PTSD Blood Transcriptome Mega-Analysis: Shared...
4     Gene Expression Signatures in Tuberculosis Hav...
5     IFN-gamma Influences Epithelial Antiviral Resp...
6     Enhancing the Biological Relevance of Machine ...
7     Altered Epithelial Gene Expression in Peripher...
8     Transcriptomic Analysis Implicates the p53 Sig...
9     Sex-specific vitamin D effects on blood coagul...
10    Systems proteomic analysis reveals that cluste...
11    Quantitative Non-canonical Amino Acid Tagging ...
12    Gene expression analysis of TIL rich HPV-drive...
13    Lithium-responsive genes and gene networks in ...
14    Upregulated Glucose Metabolism Correlates Inve...
15    Hypothalamus proteomics from mouse models with...
16    Vitamin D Metabolites Inhibit Hepatitis C Viru...
17    Structural and Functional Changes of the Invar...
18    Acute psychological stress induces short-term ...
19    The interaction of genetic determinants in the...
20    Mixed effects of suberoylanilide hydroxamic ac...
21    Dose-responsive gene expression in suberoylani...
22    Are you also what your mother eats? Distinct p...
23    Gene networks specific for innate immunity def...
24    The promise of reverse vaccinology.
25    Gender-dependent differences in plasma matrix ...
26    Blood-based gene-expression biomarkers of post...
27    Replication competent virus as an important so...
28    Whole serum 3D LC-nESI-FTMS quantitative prote...
29    Maraviroc intensification in patients with sup...

Я разбил заголовочные предложения на отдельные термины, используя это -

In[2]:    

df2 = []
for line in df1:
    for word in line.split():
        #temp = pd.DataFrame({'Title Terms' : word})
        df2.append({'Terms':word})
pd.DataFrame(df2)

, который дает мне точный вывод, который я хотел -

Out[2]:

    Terms
0   The
1   adhesins
2   of
3   non-typeable
4   Haemophilus
5   influenzae.
6   Head
7   and
8   Neck
9   Squamous
10  Cell
11  Carcinomas
12  Are
13  Characterized
14  by
15  a
16  Stable
17  Immune
18  Signature
19  Within
20  the
21  Primary
22  Tumor
23  Over
24  Time
25  and
26  Space.
27  Multitissue
28  Transcriptomics
29  Delineates

Основная проблема возникает здесь, когда я хочу, чтобы пользователь вводил любой термин, чтобы он / она получал частоту этого термина взамен, если этот термин найден в кадре данных, иначе в выходных данных должно появиться сообщение «Извините! {Термин этого пользователя Введенный} не найден в базе данных "

Я пробовал этот код -

In[3]:

from string import *
import re
from nltk import FreqDist
from collections import Counter
term = input("Enter the term you are looking for:")
term = term.lower()
#counts = Counter(term)
found = True
#database = re.findall(r'\w+', open('C:\\Users\\orevo\\Desktop\\Python\\Publication Data\\Term\\Database of Terms.txt').read().lower())
database = pd.DataFrame(df2)

for term in database:
    #record = line.split('')
    record = record
    if record == term:
       found = True
       print("Found your word")
       break
    else:
       found = False
       print("We couldn't find your term")
       continue

if found == False:
    print("Sorry!Term is not found in the database")
else:
    print("Term :"+term+"\nNumber of times this term appears in the 
    database:"+str(Counter(term))) 

но это не дает желаемого результата и дает мне это вместо этого, даже если этот термин находится в кадре данных -

Out[3]:     

Enter the term you are looking for:
In:    HIV
Out:   We couldn't find your term
       Sorry!Term is not found in the database

Прошу прощения за мои «не очень хорошие навыки программирования», если я написал что-то глупое в коде, но я новичок в Python и все еще учусь. Любая помощь с этим будет по достоинству оценена. Заранее спасибо!

Ответы [ 2 ]

0 голосов
/ 03 мая 2018

Вы можете попробовать это так:

term = raw_input("Enter the term you are looking for:").lower()
df2['Terms'] = df2['Terms'].str.lower()
frequency = df2.loc[df.Terms == term, 'Terms'].count()
if frequency == 0:
    print("Sorry!Term is not found in the database")
else:
    print("Term :"+term+"\nNumber of times this term appears in the database:"+str(frequency))
0 голосов
/ 03 мая 2018

Почему бы нам просто не использовать value_counts? Возможно, вам придется сделать немного больше очистки на Terms, чтобы убрать пунктуацию с некоторыми словами. Но как только это будет сделано, это будет хорошо работать. Используя вашу df часть выше ...

term = input("Enter the term you are looking for:")
term = term.lower()

vcounts = df.Terms.str.lower().value_counts()

try:
    print(term+':', vcounts[term])
except KeyError: 
    print('Sorry!', term, 'is not found in the database')

#Enter the term you are looking for: the
#the: 2

#Enter the term you are looking for: The
#the: 2

#Enter the term you are looking for: Oranges
#Sorry! oranges is not found in the database
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...