基于csv文件中特定列的不同值,使用python csv打印与另一列的最小值相关的所有行

基于csv文件中特定列的不同值,使用python csv打印与另一列的最小值相关的所有行,python,csv,Python,Csv,我有一个具有以下结构的CSV文件: Id,User,P_Name,P_Code,Rate 1,U1,P1,1234,21.5 2,U1,P2,7483,20 3,U1,P3,8945,29.5 4,U2,P1,1234,80 5,U2,P2,7483,23.5 6,U2,P3,8945,30 7,U3,P1,1234,15 8,U3,P2,7483,27.3 9,U3,P3,8945,,29.7 我想打印每个产品最小值的完整行。例如,这里是: 7,U3,P1,1234,15

我有一个具有以下结构的CSV文件:

Id,User,P_Name,P_Code,Rate

1,U1,P1,1234,21.5

2,U1,P2,7483,20

3,U1,P3,8945,29.5

4,U2,P1,1234,80

5,U2,P2,7483,23.5

6,U2,P3,8945,30

7,U3,P1,1234,15

8,U3,P2,7483,27.3

9,U3,P3,8945,,29.7
我想打印每个产品最小值的完整行。例如,这里是:

7,U3,P1,1234,15

2,U1,P2,7483,20

3,U1,P3,8945,29.5
我是python新手,在此之后无法继续:

import csv
with open('sample.csv', 'rb') as csvfile:
        filereader = csv.reader(csvfile, delimiter=',', quotechar='|')
        headers=next(filereader)
        data = []
        for row in filereader:
                data.append(row[2])
        print (data)

在这里,我得到了一个
p\u Name
值列表,但无法找出如何获得每个不同产品的最小值。

首先附加整个CVS行,而不仅仅是该行的第三项(如
行[2]

然后构建一个dict,使用p_name作为键,整行作为值。因此,dicts存储整行,第[2]行作为键。然后对每行进行iter,如果发现更低的价格,则用新的dict值替换当前的dict值

filter = {}
for item in data:
   if item[2] not in filter.keys():     #First if dict already has an entry in dict
           filter[item[2]] = item       #if no entry ad entry
   elif item[4] < filter[item[2]][4]:   #if entry compare between entry in dicts and cvs line. 
                                        #Both refer to [4] so booth compare the rate of the CVS column
           filter[item[2]] = item
根据您的第二句话,那么最好在值中添加额外的信息。 您可以选择一个列表,其中包含索引0 price[0]中price的数据和索引1 price[1]中的用户的数据

filter = {}
for item in data:
   if item[2] not in filter.keys():     #First if dict already has an entry in dict
           filter[item[2]] = [item[4], [item[1]]       #if no entry ad entry, the dict value is a list.
 #Filter Dict Value explained ..
 #Index 0 stores the the price of the product
 #Index 1 stores a list of users that have the product at this value. 

   elif   item[4] == filer[item[2]][0]:                #price is identical add another user to the dict[product][second slot of list]
              filter[item[2]][1].append(item[1])       #filter[productCode][second index] APPEND [New user ]



   elif item[4] < filter[item[2]][0]:   

#If a lower product rate has been found, then reset the value of the dict. 
#And store new lower price, with it's corresponding user.                                       
           filter[item[2]] = [item[4], [item[1]]
filter={}
对于数据中的项目:
如果项[2]不在filter.keys()中:#首先,如果dict在dict中已经有一个条目
过滤器[项目[2]]=[项目[4],[项目[1]]#如果没有条目,则dict值为列表。
#过滤器Dict值已解释。。
#索引0存储产品的价格
#索引1存储产品具有此值的用户列表。
elif项[4]==文件管理器[项[2]][0]:#价格相同将另一用户添加到dict[产品][列表的第二个插槽]
筛选器[项目[2]][1]。追加(项目[1])#筛选器[产品代码][第二个索引]追加[新用户]
elif项[4]<筛选器[项[2]][0]:
#如果发现较低的产品速率,则重置dict的值。
#并存储新的较低价格,与它对应的用户。
过滤器[项目[2]]=[项目[4],[项目[1]]

谢谢您的回复。我对您的代码做了一些修改,使其更简单

filter={}
对于数据中的项目:
如果项[2]不在filter.keys()中:
过滤器[项目[2]]=项目
elif项[4]==过滤器[项[2]][4]:
筛选[项目[2]]。追加(项目) elif项[4]<过滤器[项[2]][4]:
过滤器[项目[2]]=项目

但是,在从更新csv文件中的第5行(post标题)之后,我在结果的格式方面遇到了一些问题

5,U2,P2,7483,23.5 

然后使用以下代码打印结果:

 for item in filter.keys():
                print filter[item]
结果如下:

['2', 'U1', 'P2', '7483', '20', ['5', 'U2', 'P2', '7483', '20']]
['3', 'U1', 'P3', '8945', '29.5']
['7', 'U3', 'P1', '1234', '15']
然而,如果有两个用户为某一特定产品支付相同的价格,那么我希望将其显示为单独的条目,并采用与csv文件类似的格式(不带括号和引号),而不是与前一个用户一起附加这些详细信息,如:


每个产品的最小值是什么意思?为什么有两个U1?:)每个产品的最小值我指的是用户U1、U2和U3之间的产品P1的最小值。类似地,用户U1、U2和U3之间的产品P2的最小值。依此类推,产品P3。有两个U1,因为对于产品P2和P3,U1支付的值都比较低。谢谢!这是有效的非常好。非常感谢您的回复。但是,如果是任何产品(P1、P2或P3)的案例费率对于多个用户都是一样的,它跳过其中一行。理想情况下,它应该以该特定产品的最小值打印所有行。此外,如果我希望以类似方式获得最大速率,并反转预期不打印的比较,则它也会打印results@H你知道上面的答案吗?最后一张灰色表格应该有你的答案对于我的答案,请点击上箭头,这样我就可以得到它的积分。Tx,Hugo
5,U2,P2,7483,20
 for item in filter.keys():
                print filter[item]
['2', 'U1', 'P2', '7483', '20', ['5', 'U2', 'P2', '7483', '20']]
['3', 'U1', 'P3', '8945', '29.5']
['7', 'U3', 'P1', '1234', '15']
2,U1,P2,7483,20 
5,U2,P2,7483,20
3,U1,P3,8945,29.5
7,U3,P1,1234,15