[英]Print all rows related to minimum values from another column based on distinct values of a specific column from csv file using 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
值的列表, P_Name
無法弄清楚如何獲得每種不同產品的最小值。
首先附加整個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作為鍵的字典,將整行作為值。 字典因此存儲整個行,並以row [2]作為鍵。 然后遍歷每一行,如果發現較低的價格,則用新的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
並打印您的值。
for item in filter.keys():
print item,' : ',filter[item]
每秒鍾講一次,最好將附加信息添加到值中。 您可以選擇一個列表,其中包含索引0 price [0]中的價格數據和索引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 = {} 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.