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

  • 本文关键字:选择 csv 一列 python python csv pandas
  • 更新时间 :
  • 英文 :


我使用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) 返回与我感兴趣的特定主题编号相关的doc_number列表。

我对python还是个新手,不知道如何处理csv包或panda。。。

您可以先在列probability中的replace,.,然后通过astype转换为float。然后用document_number列求出groupby,用idxmax求出列probability的最大值的index。上次通过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_dicttopic_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}

另一种解决方案首先通过柱probability获得sort_values,然后通过聚集first:获得groupby

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}

最新更新