Skip to main content
 首页 » 编程设计

python之在python中与另一列具有相同值的行中选择具有最高列值的csv行

2024年10月17日5arxive

我使用 Latent Dirichlet Allocation 为 5000 多个 txt 文档创建了一个包含 20 个主题的主题模型。我现在有一个 .csv 文件,其中包含三列:文档编号、主题编号和主题在文档中的概率。它看起来像这样(对于文档 n°1 和 n°2):

1   1   0,113 
1   4   0,2 
1   7   0,156 
1   17  0,065 
1   18  0,463 
2   1   0,44 
2   6   0,207 
2   14  0,103 
2   16  0,126 
2   17  0,015 
2   18  0,106 

基本上,我想知道某个主题的文档列表,该主题最有可能出现。

我想我必须执行以下操作:

1) 为第 1 列中的每个相同值(称为 doc_number)获取第 3 列中的最大值(称为 highest_prob)。

2)对于每一个得到的doc_number(有多少文档就应该有多少),在第2列中得到对应的主题编号(称之为topic_number) .

3) 返回与我感兴趣的特定topic_number 关联的doc_number 列表。

我是 python 的新手,不知道如何继续使用 csv 包或 pandas...

请您参考如下方法:

可以先replace , 到 . 列 probability 并通过 astype 转换为 float .那么groupby按 document_number 列并获取 probability 列最大值的 index idxmax .最后通过 loc 获取所有记录:

import pandas as pd 
 
df = pd.DataFrame({'document_number': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 2, 6: 2, 7: 2, 8: 2, 9: 2, 10: 2},  
                   'probability': {0: '0,113', 1: '0,2', 2: '0,156', 3: '0,065', 4: '0,463', 5: '0,44', 6: '0,207', 7: '0,103', 8: '0,126', 9: '0,015', 10: '0,106'},  
                   'topic_number': {0: 1, 1: 4, 2: 7, 3: 17, 4: 18, 5: 1, 6: 6, 7: 14, 8: 16, 9: 17, 10: 18}},  
                    columns = ['document_number','topic_number','probability']) 
 
print (df) 
    document_number  topic_number probability 
0                 1             1       0,113 
1                 1             4         0,2 
2                 1             7       0,156 
3                 1            17       0,065 
4                 1            18       0,463 
5                 2             1        0,44 
6                 2             6       0,207 
7                 2            14       0,103 
8                 2            16       0,126 
9                 2            17       0,015 
10                2            18       0,106 
df['probability'] = df.probability.str.replace(',','.').astype(float) 
 
print (df.groupby('document_number')['probability'].idxmax()) 
1    4 
2    5 
Name: probability, dtype: int64 
 
print (df.loc[df.groupby('document_number')['probability'].idxmax()]) 
   document_number  topic_number  probability 
4                1            18        0.463 
5                2             1        0.440 

最后set_index来自 document_number 列并转换为 to_dict列 topic_number:

print (df.loc[df.groupby('document_number')['probability'].idxmax()] 
         .set_index('document_number')['topic_number']) 
 
document_number 
1    18 
2     1 
Name: topic_number, dtype: int64 
 
print (df.loc[df.groupby('document_number')['probability'].idxmax()] 
         .set_index('document_number')['topic_number'].to_dict()) 
{1: 18, 2: 1} 

另一种解决方案优先sort_values按 probability 列,然后是 groupby聚合first :

print (df.sort_values(by="probability", ascending=False) 
         .groupby('document_number', as_index=False) 
         .first()) 
 
   document_number  topic_number  probability 
0                1            18        0.463 
1                2             1        0.440 
 
print (df.sort_values(by="probability", ascending=False) 
         .groupby('document_number', as_index=False) 
         .first().set_index('document_number')['topic_number']) 
 
document_number 
1    18 
2     1 
Name: topic_number, dtype: int64 
 
print (df.sort_values(by="probability", ascending=False) 
         .groupby('document_number', as_index=False) 
         .first().set_index('document_number')['topic_number'].to_dict()) 
{1: 18, 2: 1}