[英]Add specific row data for a specific column value
Hi everyone, I'm new to python.大家好,我是python的新手。 When I find a specific value in the column comm_rate
, I want to add the utility_name
and zip
to print out, like the example below.当我发现在列的特定值comm_rate
,我想补充的utility_name
和zip
打印出来,如下面的例子。 Any help will be highly appreciated.任何帮助将不胜感激。
Here is the link for the CSV file :这是 CSV文件的链接:
The highest rate is: Napakiak Ircinraq Power Co (99634, AK)
- $0.839779005525
最高利率是: Napakiak Ircinraq Power Co (99634, AK)
- $0.839779005525
Here is my code and CSV image attached:这是我的代码和附加的 CSV 图像:
import csv
def pronmpt_filename():
filename = input("Please enter the data file: ")
return filename
def comm_rate():
commR_list = []
with open(pronmpt_filename(), "r") as my_csv:
reader = csv.DictReader(my_csv, delimiter=",")
for row in reader:
commR = row["comm_rate"]
if commR != None and commR != "":
commR_list.append(float(row["comm_rate"]))
avg_commR = sum(commR_list) / len(commR_list)
print("")
print("The average commercial rate is: ", avg_commR)
print("")
print("The highest rate is: \n", max(commR_list))
print("")
print("The lowest rate is: \n", min(commR_list))
You can do it using python zip
function.您可以使用 python zip
函数来完成。 Here I show you the example (I will be using pandas
to read the csv file, you can choose as per your requirement. But I will recommend pandas
for this type of problems):在这里,我向您展示示例(我将使用pandas
读取 csv 文件,您可以根据您的要求进行选择。但我会推荐pandas
来解决此类问题):
import pandas as pd
read_file = pd.read_csv("filename.csv")
zipped_result = [ (f, i) for f, i in zip(read_file["comm_rate"], read_file["utility_name"])]
for i,j in zipped_result:
print(f"The highest rate is: {j} - {i}.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.