Skip to main content
 首页 » 编程设计

python之遍历每一行并增加 csv 文件中与特定研究人员相关的术语的计数

2025年05月04日13findumars

我正在阅读 python 中的 csv 文件,其中一列中有许多疾病名称,另一列中有相关研究人员。该文件看起来像这样 -

[Table 1] 
Terms                    Researcher 
1.Asthma                 Dr. Roberts 
2.Brochial cancer        Dr. Lee 
3.HIV                    Dr.Roberts 
4.HIV                    Dr. Lee 
5.Influenzae             Dr. Wang 
6.Bronchial Cancer       Dr. Wang 
7.Influenzae             Dr. Roberts 
8.dengue                 prof. christopher 
9.Arthritis              prof. swaminathan 
10.Arthritis             prof. christopher 
11.Asthma                Dr. Roberts 
12.HIV                   Dr. Lee 
13.Bronchial Cancer      Dr. Wang 
14.dengue                prof. christopher 
15.HIV                   prof. christopher 
16.HIV                   Dr. Lee 

我希望我的代码遍历每一行并增加与每个研究人员相关的术语的频率计数,以便当用户输入他/她正在寻找的术语时,他们应该得到这样的输出表 -

Term you are looking for : HIV 
Names of the researchers                Frequency 
Dr. Roberts                             1 
Dr. Lee                                 3 
prof. christopher                       1 

现在让我们看看我在做什么-

In[1]: 
import pandas as pd 
import numpy as np 
data = pd.read_csv("Researchers Title Terms.csv") 
data.head() 

这给了我 [表 1] 然后我这样做 -

In[2]: 
term = input("Enter the term you are looking for:") 
term = term.lower() 
list_of_terms = [] 
for row in data:  
    if row[data.Terms] == term 
        researcher1 += 1 
 
    elif data.Terms == term 
        researcher2 += 1 
 
    elif data.Terms == term 
        researcher3 += 1 
 
    else 
        print("Sorry!", term, "not found in the database!") 
print("Term you are looking for : ", term) 
print("Dr. Roberts:", researcher1) 
print("Dr. Lee:", researcher2) 
print("prof. christopher:", researcher3) 

我得到的只是-

File "<ipython-input-9-b85d0d187059>", line 5 
if row[data.Terms] == term 
                          ^ 
SyntaxError: invalid syntax 

我是 python 编程的初学者,所以不太确定我的逻辑是否完全错误或者这里确实存在一些语法错误。任何帮助将不胜感激。在尝试了一些事情并没有得到任何输出之后,我将其发布到社区中。 提前致谢!

请您参考如下方法:

groupby 和 value_counts

简单直观

df.Terms = df.Terms.str.replace('\d+\.\s*', '').str.upper() 
df.Researcher = df.Researcher.str.title() 
s = df.groupby('Terms').Researcher.value_counts() 
 
s 
 
Terms             Researcher        
ARTHRITIS         Prof. Christopher    1 
                  Prof. Swaminathan    1 
ASTHMA            Dr. Roberts          2 
BROCHIAL CANCER   Dr. Lee              1 
BRONCHIAL CANCER  Dr. Wang             2 
DENGUE            Prof. Christopher    2 
HIV               Dr. Lee              3 
                  Dr.Roberts           1 
                  Prof. Christopher    1 
INFLUENZAE        Dr. Roberts          1 
                  Dr. Wang             1 
Name: Researcher, dtype: int64 

您可以使用 loc 或 xs 访问不同的条款

s.loc['HIV'] 
 
Researcher 
Dr. Lee              3 
Dr.Roberts           1 
Prof. Christopher    1 
Name: Researcher, dtype: int64 

或者

s.xs('HIV') 
 
Researcher 
Dr. Lee              3 
Dr.Roberts           1 
Prof. Christopher    1 
Name: Researcher, dtype: int64 

pd.factorize 和 np.bincount

import re 
 
pat = re.compile('\d+\.\s*') 
f, u = pd.factorize(list(zip( 
    (re.sub(pat, '', x).upper() for x in df.Terms), 
    df.Researcher.str.title() 
))) 
 
s = pd.Series(dict(zip(u, np.bincount(f)))) 

访问方式同上。