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

时间:2016-11-09 18:53:05

标签: 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

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_Name值的列表,但无法弄清楚如何获得每种不同产品的最小值。

2 个答案:

答案 0 :(得分:0)

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

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)
    print (data)

然后构建一个使用P_name作为键的dict,整行作为值。因此,dicts存储整行,其中row [2]为关键。然后在每一行上,如果找到更低的价格,用新的替换当前的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

打印你的价值观。

 for item in filter.keys():
      print item,' : ',filter[item]

根据您的第二个评论,最好是为值添加额外信息。 您可以选择一个列表,其中包含索引0价格[0]和索引1中的用户,价格[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]]

答案 1 :(得分:0)

感谢您的回复。我稍微修改了你的代码,使它更简单。

filter = {} for item in data: if item[2] not in filter.keys():
filter[item[2]] = item
elif item[4] == filter[item[2]][4]:
filter[item[2]].append(item) elif item[4] < filter[item[2]][4]:
filter[item[2]] = item

虽然,它工作正常。但是,在更新csv文件中的第5行(帖子标题)后,我面临一些格式化问题的问题

5,U2,P2,7483,23.5 

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']

如果有两个用户为特定产品支付相同的价格,那么我不想将这些细节与之前的用户一起附加,而是希望将其显示为单独的条目,格式与csv文件类似(不带括号和引号),如:

2,U1,P2,7483,20 
5,U2,P2,7483,20
3,U1,P3,8945,29.5
7,U3,P1,1234,15